How you're running this code?
When I run it, I get this output:
▶ dart test.dart
start
Unhandled exception:
type 'int' is not a subtype of type 'String'
#0 main (file:///Users/renato/programming/projects/test.dart:6:3)
As I expected.
What Dart version are you using (run dart --version
)? Make sure you have an up-to-date version like 2.10
.
If you still have issues, you might have a local analysis_options.yaml
file which overrides the Dart compiler checks to be more lenient, in which case it might not check the types (I am not sure which option would enable this, but I suppose it's possible).
Check how analysis options work here.
I suggest you always enable "strong mode" by adding this to your analysis options file:
analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false
By the way, if you want to try the new Dart NNBD (Not-Null-By-Default) experimental feature, you need to use the Dart dev channel releases and run your program with dart --enable-experiment=non-nullable file.dart
.
To learn how to enable NNBD, check the null-safety tech preview 2 blog post about it.