Junior — Middle
What functions and tools for working with null safety were introduced in Dart 2.12?
sobes.tech AI
Answer from AI
In Dart 2.12, support for null safety was introduced — a system that helps avoid errors related to null values. Main features and tools:
- Nullable and non-nullable types: by default, types cannot be null to prevent NullPointerException errors. To allow null, use
?, for example,String?. - Null check operators:
!(assert non-null),??(if null, then),?.(safe call for method or property). - Compile-time analysis: the compiler checks that variables are not used without initialization or null checks.
Example:
String? name; // can be null
void greet() {
if (name != null) {
print('Hello, ${name!}'); // ! tells the compiler that name is not null
} else {
print('Hello, guest');
}
}