Basic Dart language
A basic Dart program
void main() {
for (int i = 0; i < 5; i++) {
printInteger(i);
}
}
void printInteger(int myNumber) {
print("My number is $myNumber");
}
Variables
Type inference
void main() {
var name = 'Truong'; // Type inference to String
var age = 18; // Type inference to int
print(name.runtimeType);
print(age.runtimeType);
}
Null Safety
void main() {
print(guessGender("Truong"));
print(guessGender("Unknown"));
print(guessGender(null));
}
String? guessGender(String? name) {
print("Input name is ${name!}");
if (name == "Truong") {
return "Male";
}
}
Late variables
- Declaring a non-nullable variable that’s initialized after its declaration.
- Lazily initializing a variable.
late String name; // 1. a non-nullable variable that’s initialized after its declaration.
void main() {
late String temperature = _readThermometer(); // 2. Lazily initialized. If temperature is never used, _readThermometer() is never called
}
String _readThermometer() {
// E.g. the function is very costly
return "";
}
When to use lazily initializing variable?
- The variable might not be needed, and initializing it is costly.
- You’re initializing an instance variable, and its initializer needs access to this.
static, final and const
-
“static” means a member is available on the class itself instead of on instances of the class. That’s all it means, and it isn’t used for anything else. static modifies members.
-
“final” means single-assignment: a final variable or field must have an initializer. Once assigned a value, a final variable’s value cannot be changed. final modifies variables.
-
“const” has a meaning that’s a bit more complex and subtle in Dart. const modifies values. You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3). Here, const means that the object’s entire deep state can be determined entirely at compile time and that the object will be frozen and completely immutable.