Middle
What new features appeared in Dart 2.12?
sobes.tech AI
Answer from AI
- Null safety (null-soundness): It is guaranteed that variables cannot contain null unless their type is declared as nullable (with a
?suffix). - Non-nullable by default: All types, except those explicitly declared as nullable (with
?), now do not allow null. - Flow analysis: The code analyzer has become smarter in determining when a nullable variable is guaranteed not to contain null.
- Type inference improvements: Type inference has been improved, especially in the context of null safety.
// Null Safety example
String nonNullableString = 'hello'; // Cannot be null
String? nullableString = null; // Can be null
void printLength(String? text) {
if (text != null) {
print(text.length); // flow analysis guarantees that text is not null here
}
}