Episode 2: Exploring Dart: Variables, Null Safety, Default Values, Late Variables, and Final & Const
Season 2: Exploring Dart #dartforbeginners
In the world of programming, Dart has carved a unique space for itself, and understanding its constructs can help you develop more efficient and safer apps. In this article, we're exploring five essential Dart concepts: Variables, Null Safety, Default Values, Late Variables, and Final & Const.
Dart Variables
In Dart, a variable is a storage location that holds a value. Variables store references to the data. For instance:
var name = 'Bob';
In this example, we've created a variable name
and assigned it a string value 'Bob'
. The variable name
now refers to a String object containing the value 'Bob'.
It's possible to explicitly declare the type of a variable:
Object name = 'Bob';
String name = 'Bob';
In these examples, we've clarified that name
is of type Object
or String
.
Null Safety in Dart
A standout feature of Dart is Null safety, a mechanism that helps prevent null dereference errors. These errors occur when you access a property or call a method on a null object. Null safety in Dart helps catch potential null errors during compile time.
You can specify a variable's nullability in Dart using the ?
symbol:
String? name; // Nullable
String name; // Non-nullable
Appending ?
to a type declares the variable as nullable, meaning it can contain a null value. Without the ?
, the variable is non-nullable and must always hold a non-null value.
Default Values in Dart
In Dart, any uninitialized variable with a nullable type gets an initial value of null:
int? lineCount;
assert(lineCount == null);
In the example above, the variable lineCount
has a default value of null because it's declared as nullable. For non-nullable variables, they must be initialized before using:
int lineCount = 0;
Here, lineCount
is initialized with a zero value upon declaration.
Dart’s Late Variables
Dart introduces a keyword late
that enables declaring a non-nullable variable to be initialized after its declaration. It also allows for lazily initializing a variable, i.e., initializing it the first time it's used.
late String description;
void main() {
description = 'Feijoada!';
print(description);
}
In the code above, we've used the late
keyword, indicating to Dart that we will initialize the description
variable at a later stage.
Final and Const in Dart
For variables that you don't want to change once set, Dart provides final
and const
keywords.
final name = 'Bob';
const bar = 1000000;
A final
variable can only be set once and cannot be changed after it's been set. A const
variable is for variables meant to be compile-time constants. All const
variables are implicitly final
.
Conclusion
Dart offers a rich and robust set of features for managing variables and their nullability. By preventing null errors, providing keywords for late initialization, and offering immutable variables via final
and const
, Dart ensures the safety and efficiency of your applications. A strong understanding of these concepts will considerably enhance your Dart coding skills. Keep exploring and happy coding!