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 = 42
is "Inferred to be anint
" and notint?
.In your example, what happens is that
var x = null
are resolved sox
are seen as the typedynamic
since Dart have no clue about what type you are trying to use. Sincedynamic
can have the valuenull
you are good to go.