Overview of Built-in Types
Dart provides a set of basic primitive types similar to other modern languages, including:
- Numbers
- Strings
- Booleans
- Lists (Arrays)
- Maps
- Runes (Unicode character representation)
- Symbols
A key characteristic of these types is that they can be instantiated using literal syntax. While most types allow constructor initialization, num and bool types do not have publicly accessible constructors.
Numeric Types
Dart distinguishes between integer and floating-point values using int and double. When running on the Dart Native VM, an int ranges from -263 to 263 - 1. However, when compiled to JavaScript, the range aligns with JS limits: -253 to 253 - 1.
import 'dart:math' as math;
void main() {
int wholeNumber = 10;
// int errorExample = 10.5; // This causes an error
double floatingPoint = 10.5;
double autoCast = 10; // Automatic conversion in Dart 2.1+
var hexValue = 0xDEADBEEF; // Hexadecimal support
// Handling overflow/large numbers
print(math.pow(2, 100));
}
Conversion between types is explicit. Use toString() for numbers to strings, and int.parse() or double.parse() for the reverse. Unlike JavaScript, Dart does not perform implicit type coercion.
void conversionExamples() {
// Number to String
var numVal = 55.123;
print(numVal.toString());
print(numVal.toStringAsFixed(1)); // "55.1"
print(numVal.toStringAsExponential(2)); // "5.51e+1"
print(numVal.toStringAsPrecision(4)); // "55.12"
// String to Number
String strVal = "99";
print(int.parse(strVal));
print(double.parse("10.5"));
}
Beyond standard arithmetic, complex calculations can be handled via the dart:math library.
String Manipulation
Dart strings support both single and double quotes. Multi-line strings are defined using triple quotes. String concatenation occurs automatically when ajdacent string literals are placed next to each other.
void stringLiterals() {
var single = 'Single quotes';
var double = "Double quotes";
var multiLine = """
This is a
multiline block.
""";
var combined = 'Hello ' "World"; // Valid concatenation
}
You can generate strings via constructors for charcater codes:
void constructors() {
print(String.fromCharCode(66)); // B
print(String.fromCharCodes([72, 69, 76, 76, 79])); // HELLO
}
String methods in Dart close mirror JavaScript's API:
void stringMethods() {
String text = "Dart-2024";
print(text.length); // 9
print(text.substring(0, 4)); // Dart
print(text.codeUnitAt(0)); // 68 (Unicode value)
print(text.startsWith("D")); // true
print(text.endsWith("2024")); // true
print("a-b-c".replaceAll("-", ":")); // a:b:c
print("a-b-c".replaceFirst("-", ":")); // a:b-c
print("one,two".split(",")); // [one, two]
print(" trim me ".trim()); // "trim me"
print(text.contains("202")); // true
print(text.indexOf("-")); // 4
}
Dart supports string interpolation. While ${expression} is standard, you can omit the braces for simple variable insertion using $variable.
void interpolation() {
var lang = "Dart";
var result = "$lang has ${lang.length} characters."; // "Dart has 4 characters."
}
Boolean Type
The bool type holds either true or false. Dart is type-safe; unlike JavaScript, values like 0 or empty strings are not treated as false in a boolean context. Logical operators require boolean operands explicitly.
void checkBooleans() {
bool isActive = true;
bool isDone = false;
print(!isActive); // false
print(isActive && isDone); // false
print(isActive || isDone); // true
// The following would cause a compile-time error:
// if (1) { print("No implicit conversion"); }
}