When upgrading a Flutter package for null safety I got this error when running flutter test
:
Error: A library can't opt out of null safety by default, when using sound null safety.
// @dart = 2.8
^^^^^^^^^^^^^^
Failed to load "/Users/xxxxx/test/text_test.dart":
Compilation failed
The answer was right there but I was still confused for a while, so I'm adding my answer below.
The problem also occurs when you have a mixed-version code i.e. the code is not fully migrated to sound null safety. To run the app:
Using IDE
Add
// @dart=2.9
at the top in yourmain.dart
file and run the app using the Play ► icon.Using command line
or to be specific (say in chrome)