Error: A library can't opt out of null safety by default, when using sound null safety

Issue

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.

Solution

To solve your problem, remove the following line from text_test.dart:

// @dart = 2.8

This annotation is for opting out of sound null safety (see this and this). However, libraries (packages) are not allowed to do that if they want to be sound null safe.

Answered By – Suragch

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.