I'm copying this from the docs
// In null-safe Dart, none of these can ever be null.
var i = 42; // Inferred to be an int.
String name = getFileName();
final b = Foo();
But I'm running the code below in a null-safe dartpad, and it compiles.
void main() {
var x = null;
print(x);
}
Is this a documentation error or am I missing something?
Your example are not close to what the documentation are trying to explain. Try this instead:
The reason is that
var x = 42is "Inferred to be anint" and notint?.In your example, what happens is that
var x = nullare resolved soxare seen as the typedynamicsince Dart have no clue about what type you are trying to use. Sincedynamiccan have the valuenullyou are good to go.