Dart Built-in Functions
Essential functions available in Dart's core library
⚡ What are Built-in Functions?
Dart built-in functions are pre-defined methods available in the core library that perform common operations like printing, type conversion, mathematical calculations, and string manipulation.
// Common built-in functions
void main() {
print('Hello World'); // Output function
int number = int.parse('42'); // String to int
String text = number.toString(); // Int to string
print('Number: $text');
}
Output:
Hello World
Number: 42
Function Categories
Output Functions
Display data to console
print('Hello');
print(42);
print([1, 2, 3]);
Type Conversion
Convert between data types
int.parse('123');
double.parse('3.14');
'42'.toString();
Math Functions
Mathematical operations
import 'dart:math';
max(10, 20);
min(5, 3);
sqrt(16);
Validation
Check and validate data
assert(5 > 3);
identical(a, b);
'text'.isEmpty;
🔹 Output Functions
Functions to display information:
// print() - most common output function
print('Simple text');
print(42);
print([1, 2, 3, 4, 5]);
print({'name': 'John', 'age': 30});
// stdout.write() - print without newline
import 'dart:io';
stdout.write('Hello ');
stdout.write('World');
print(''); // Add newline
// stderr.writeln() - print to error stream
stderr.writeln('This is an error message');
Output:
Simple text
42
[1, 2, 3, 4, 5]
{name: John, age: 30}
Hello World
🔹 Type Conversion Functions
Convert data between different types:
🔸 String Conversions
// String to numbers
String numberStr = '42';
String floatStr = '3.14';
int intValue = int.parse(numberStr); // 42
double doubleValue = double.parse(floatStr); // 3.14
// Numbers to string
int age = 25;
double price = 19.99;
String ageStr = age.toString(); // '25'
String priceStr = price.toString(); // '19.99'
String formatted = price.toStringAsFixed(1); // '20.0'
🔸 Safe Conversions
// tryParse returns null if conversion fails
int? result1 = int.tryParse('abc'); // null
int? result2 = int.tryParse('123'); // 123
if (result1 != null) {
print('Valid number: $result1');
} else {
print('Invalid number format');
}
🔹 Mathematical Functions
Built-in math operations from dart:math:
import 'dart:math';
// Basic math functions
print(max(10, 20)); // 20 - maximum value
print(min(5, 15)); // 5 - minimum value
print(pow(2, 3)); // 8.0 - power (2^3)
print(sqrt(16)); // 4.0 - square root
// Rounding functions
double value = 3.7;
print(value.round()); // 4 - round to nearest
print(value.floor()); // 3 - round down
print(value.ceil()); // 4 - round up
// Random numbers
Random random = Random();
print(random.nextInt(10)); // Random int 0-9
print(random.nextDouble()); // Random double 0.0-1.0
// Constants
print(pi); // 3.141592653589793
print(e); // 2.718281828459045
🔹 String Functions
Built-in string manipulation methods:
String text = 'Hello World';
// Length and checking
print(text.length); // 11
print(text.isEmpty); // false
print(text.isNotEmpty); // true
// Case conversion
print(text.toLowerCase()); // 'hello world'
print(text.toUpperCase()); // 'HELLO WORLD'
// Searching
print(text.contains('World')); // true
print(text.startsWith('Hello')); // true
print(text.endsWith('World')); // true
print(text.indexOf('o')); // 4
// Modification
print(text.replaceAll('o', '0')); // 'Hell0 W0rld'
print(text.substring(0, 5)); // 'Hello'
print(text.split(' ')); // ['Hello', 'World']
// Trimming
String padded = ' Hello ';
print(padded.trim()); // 'Hello'
🔹 List Functions
Built-in list manipulation methods:
List numbers = [1, 2, 3, 4, 5];
// Basic operations
print(numbers.length); // 5
print(numbers.isEmpty); // false
print(numbers.first); // 1
print(numbers.last); // 5
// Adding elements
numbers.add(6); // [1, 2, 3, 4, 5, 6]
numbers.addAll([7, 8]); // [1, 2, 3, 4, 5, 6, 7, 8]
numbers.insert(0, 0); // [0, 1, 2, 3, 4, 5, 6, 7, 8]
// Removing elements
numbers.remove(0); // Remove first occurrence of 0
numbers.removeAt(0); // Remove element at index 0
numbers.removeLast(); // Remove last element
// Searching
print(numbers.contains(3)); // true
print(numbers.indexOf(3)); // Index of first occurrence
// Functional operations
List doubled = numbers.map((n) => n * 2).toList();
List evens = numbers.where((n) => n % 2 == 0).toList();
int sum = numbers.reduce((a, b) => a + b);
🔹 Validation Functions
Functions to check and validate data:
// assert() - debug-time validation
void validateAge(int age) {
assert(age >= 0, 'Age cannot be negative');
assert(age <= 150, 'Age seems unrealistic');
print('Valid age: $age');
}
// identical() - check if two objects are the same
String a = 'hello';
String b = 'hello';
String c = a;
print(identical(a, b)); // false (different objects)
print(identical(a, c)); // true (same object)
print(a == b); // true (same content)
// Type checking with is and is!
dynamic value = 42;
print(value is int); // true
print(value is String); // false
print(value is! String); // true
// Null checking
String? nullableText;
print(nullableText?.isEmpty ?? true); // true (null-aware)
🔹 DateTime Functions
Working with dates and times:
// Current date and time
DateTime now = DateTime.now();
print(now); // 2024-01-15 14:30:45.123
// Creating specific dates
DateTime birthday = DateTime(1990, 5, 15);
DateTime parsed = DateTime.parse('2024-01-15');
// Formatting and components
print(now.year); // 2024
print(now.month); // 1
print(now.day); // 15
print(now.hour); // 14
print(now.minute); // 30
// Date arithmetic
DateTime tomorrow = now.add(Duration(days: 1));
DateTime lastWeek = now.subtract(Duration(days: 7));
Duration difference = tomorrow.difference(now);
print('Hours until tomorrow: ${difference.inHours}');