Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 833
Suragch Avatar asked Nov 27 '20 04:11

Suragch


People also ask

How do you fix a null safety error in Flutter?

Copy: --no-sound-null-safety and add into "additional run args". Run your app with "Run/Play" Button or from "Run" Menu. In this way, you can solve "Error: Cannot run with sound null safety, because the following dependencies don't support null safety" error on Flutter project.


Video Answer


2 Answers

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:

  1. Using IDE

    Add // @dart=2.9 at the top in your main.dart file and run the app using the Play ► icon.

    // @dart=2.9
    import 'package:flutter/material.dart';
    
    void main() {
      //...
    }
    
  2. Using command line

    flutter run --no-sound-null-safety
    

    or to be specific (say in chrome)

    flutter run -d chrome --no-sound-null-safety
    
like image 178
CopsOnRoad Avatar answered Oct 12 '22 08:10

CopsOnRoad


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.

like image 45
Suragch Avatar answered Oct 12 '22 09:10

Suragch