Dart Core Library

Essential built-in functionality for Dart programming

🎯 What is Dart Core Library?

Dart Core Library provides fundamental classes and functions for all Dart programs. It includes basic data types, collections, and essential utilities that form the foundation of Dart programming.


// Core library is imported automatically
void main() {
  String message = 'Hello Core!';
  List numbers = [1, 2, 3];
  print('$message Numbers: $numbers');
}
                                    

Output:

Hello Core! Numbers: [1, 2, 3]

Core Data Types

🔤

String

Text manipulation and operations

String name = 'Dart';
print(name.toUpperCase());
print(name.length);
🔢

Numbers

int and double for numeric values

int age = 25;
double price = 99.99;
print(age + price.round());
✅

bool

Boolean true/false values

bool isActive = true;
bool isEmpty = false;
print(isActive && !isEmpty);
📋

Collections

List, Set, and Map data structures

List fruits = ['apple', 'banana'];
Map scores = {'Alice': 95};

🔹 String Operations

Common string manipulations:

void main() {
  String text = 'Hello Dart World';
  
  // Basic operations
  print('Length: ${text.length}');
  print('Uppercase: ${text.toUpperCase()}');
  print('Lowercase: ${text.toLowerCase()}');
  
  // String methods
  print('Contains "Dart": ${text.contains('Dart')}');
  print('Starts with "Hello": ${text.startsWith('Hello')}');
  print('Replace: ${text.replaceAll('World', 'Universe')}');
  
  // String interpolation
  String name = 'Alice';
  int age = 30;
  print('Name: $name, Age: $age');
}

Output:

Length: 17

Uppercase: HELLO DART WORLD

Lowercase: hello dart world

Contains "Dart": true

Starts with "Hello": true

Replace: Hello Dart Universe

Name: Alice, Age: 30

🔹 List Operations

Working with lists (arrays):

void main() {
  List numbers = [1, 2, 3, 4, 5];
  
  // Basic operations
  print('List: $numbers');
  print('Length: ${numbers.length}');
  print('First: ${numbers.first}');
  print('Last: ${numbers.last}');
  
  // Adding elements
  numbers.add(6);
  numbers.addAll([7, 8]);
  print('After adding: $numbers');
  
  // List methods
  print('Contains 3: ${numbers.contains(3)}');
  print('Index of 4: ${numbers.indexOf(4)}');
  
  // Filtering and mapping
  var evenNumbers = numbers.where((n) => n % 2 == 0).toList();
  print('Even numbers: $evenNumbers');
}

Output:

List: [1, 2, 3, 4, 5]

Length: 5

First: 1

Last: 5

After adding: [1, 2, 3, 4, 5, 6, 7, 8]

Contains 3: true

Index of 4: 3

Even numbers: [2, 4, 6, 8]

🔹 Map Operations

Working with key-value pairs:

void main() {
  Map ages = {
    'Alice': 25,
    'Bob': 30,
    'Charlie': 35
  };
  
  // Basic operations
  print('Ages: $ages');
  print('Alice age: ${ages['Alice']}');
  print('Keys: ${ages.keys.toList()}');
  print('Values: ${ages.values.toList()}');
  
  // Adding and updating
  ages['David'] = 28;
  ages['Alice'] = 26; // Update existing
  print('Updated: $ages');
  
  // Map methods
  print('Contains Alice: ${ages.containsKey('Alice')}');
  print('Contains age 30: ${ages.containsValue(30)}');
  
  // Iterating
  ages.forEach((name, age) {
    print('$name is $age years old');
  });
}

Output:

Ages: {Alice: 25, Bob: 30, Charlie: 35}

Alice age: 25

Keys: [Alice, Bob, Charlie]

Values: [25, 30, 35]

Updated: {Alice: 26, Bob: 30, Charlie: 35, David: 28}

Contains Alice: true

Contains age 30: true

Alice is 26 years old

Bob is 30 years old

Charlie is 35 years old

David is 28 years old

🔹 DateTime and Duration

Working with dates and time:

void main() {
  // Current date and time
  DateTime now = DateTime.now();
  print('Current time: $now');
  
  // Creating specific dates
  DateTime birthday = DateTime(1990, 5, 15);
  print('Birthday: $birthday');
  
  // Date operations
  DateTime tomorrow = now.add(Duration(days: 1));
  DateTime lastWeek = now.subtract(Duration(days: 7));
  
  print('Tomorrow: $tomorrow');
  print('Last week: $lastWeek');
  
  // Duration calculations
  Duration age = now.difference(birthday);
  print('Age in days: ${age.inDays}');
  print('Age in years: ${(age.inDays / 365).floor()}');
}

🧠 Test Your Knowledge

Which method converts a string to uppercase in Dart?