Nullability in Dart | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4921688-programming-in-dart-fundamentals/lessons/8

Looks like it is not the case anymore in dart.
Declaring the variable without initializing like this leads to compilation error:

  int age;
  print(age);
Error: Non-nullable variable 'age' must be assigned before it can be used.
  print(age);
        ^^^
Error: Compilation failed.

though this work:

int? age;

Hi @robmoorekinduct, thank you.

Same here, I’m running: Dart SDK version: 2.13.4 (stable).

The official documentation has an explanation for this, but, for some reason if I try to add the link here, my post got hidden.

So, you can google it as: dart nullable int

Bye!

This doesn’t work:

void main() {
  String err;
  var error = err ?? "This is an error";
  print(error);
}
test.dart:3:15: Warning: Operand of null-aware operation '??' has type 'String' which excludes null.
  var error = err ?? "This is an error";
              ^
test.dart:3:15: Error: Non-nullable variable 'err' must be assigned before it can be used.
  var error = err ?? "This is an error";