215

I have followed "Enabling null safety" on dart.dev and also migrated my whole Flutter application to null safety.

Now, I am trying to run it using flutter run. However, it will not start because of the following error:

Error: Cannot run with sound null safety, because the following dependencies
don't support null safety:

 - package:cloud_firestore_web
 - package:firebase_core_web
 - package:shared_preferences
 - package:url_launcher_web
 - package:firebase_auth
 - package:http
 - package:provider
...

For solutions, see https://dart.dev/go/unsound-null-safety
Failed to compile application.

The guide at the URL says that I should "wait for dependencies to migrate before you migrate your package", but I want to use non-nullable by default (NNBD) now.

How can I do that?

17 Answers 17

348

First, you should read through the guide to understand unsound null safety. If you are sure that you want to run your application with unsound null safety, you can use the following command:

flutter run --no-sound-null-safety

The --no-sound-null-safety option is not documented in the article, however, I have not experienced any problems with it for the last few months (and especially not since the whole Flutter framework has been migrated to null safety).

The documentation has now been updated to include this. See Testing or running mixed-version programs.

IDE run arguments/configuration

To set this up in your IDE of choice, you can use:

  • In IntelliJ/Android Studio: "Edit Configurations" (in your run configurations) → "Additional run args".
  • In Visual Studio Code: search for "Flutter run additional args" in your user settings.

In both cases, add --no-sound-null-safety.

Test configuration

For tests, you will want to do the same thing:

  • In IntelliJ/Android Studio: "Edit Configurations" (in your run configurations) → "Additional args".
  • In Visual Studio Code: search for "Flutter test additional args" in your user settings.

In both cases, add --no-sound-null-safety.

4
  • 1
    @ChiragArora you can add it to the additional arguments in your run configuration (click on the dropdown where it says main.dart next to the device and then on "Edit configurations"). Commented Mar 13, 2021 at 15:35
  • 1
    it it possible add fastlane build with no-sound-null-safety parameter? @creativecreatorormaybenot
    – Dolphin
    Commented Mar 14, 2021 at 8:33
  • not works in flutter 2.2.x when build a ios package @creativecreatorormaybenot stackoverflow.com/questions/67914266/…
    – Dolphin
    Commented Jun 13, 2021 at 11:36
  • Saved my life with the VSCode tip - also setting Flutter test additional args (note "test"!!) - that makes the unit tests work. Thanks!
    – n13
    Commented Sep 22, 2021 at 1:51
155

In Android Studio:

Run → Edit ConfigurationsAdd Additional Run args--no-sound-null-safety

Enter image description here

2
  • in flutter 2.2.x when build ios it works? @issac stackoverflow.com/questions/67914266/…
    – Dolphin
    Commented Jun 13, 2021 at 11:37
  • This didn't work " dC:\src\flutter\flutter\flutter>flutter run --no-sound-null-safety Downloading Web SDK... 120.8s Error: No pubspec.yaml file found. This command should be run from the root of your Flutter project." but a configuration worked : Run --> Edit Configurations --> Add Additional Run args --> --no-sound-null-safety Commented Sep 5, 2021 at 19:44
59

Easy Solution

If you are using Visual Studio Code, then go to:

  • Menu FilePreferencesSettings

  • Search for "Flutter run additional args"

  • Then click Add Item

  • Now type --no-sound-null-safety

  • Click OK.

0
55

You run into this error if your code is not fully migrated to null-safety. You can run your "mixed-version" code:

  • Using the Android Studio IDE

    Copy: --no-sound-null-safety

    Enter image description here

    enter image description here

  • In the Dart file

    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() {
      //...
    }
    
  • Using the command line

    flutter run --no-sound-null-safety
    

    Or to be specific (say in Chrome)

    flutter run -d chrome --no-sound-null-safety
    
0
50

If using Visual Studio Code, create file .vscode/launch.json in the project root and add:

"args": [
         "--no-sound-null-safety"
        ]

Complete code:

{
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
                {
                        "name": "YOUR_PROJECT_NAME",
                        "program": "lib/main.dart",
                        "request": "launch",
                        "type": "dart",
                        "args": [
                                "--no-sound-null-safety"
                            ]
                }
        ]
}
18

In case you were using Visual Studio Code and faced it in your unit test.

Visual Studio Code → PreferencesSettingsSearch setting, type in "flutter test"Dart: Flutter Test Additional Args, Add itemAdd "--no-sound-null-safety"

--no-sound-null-safety description picture

1
  • 3
    That would be a correct answer if it was for Flutter Test. But if you are getting an error while running that you should add the Additional Args for Flutter Run. so search for Flutter Run in Settings. and then add like above one.
    – Ayush Oli
    Commented Oct 4, 2021 at 8:02
9

If you want run your project with --no-sound-null-safety, you can add this line to your main.dart file at the top (first line) with a comment...

// @dart=2.9

Then your project runs with --no-sound-null-safety...

1
  • In my case, I have added " // @dart=2.9"; however, in TEST\main.dart
    – GtdDev
    Commented Jul 23, 2021 at 14:54
8

Run

dart pub outdated --mode=null-safety

in a terminal and if there is a development dependency update then update the dependency.

That might help.

7

The problem happens because the Flutter framework (version 2.2.0 and up) is now supporting sound null safety out of the box, but there are plenty of package and plugins on pub.dev are not migrated to null safety yet, so that's raising the error whenever you run a build or run command.

To overcome this issue, add the flag --no-sound-null-safety in your command.

Example:

flutter build [Target] --no-sound-null-safety

Target arguments:

For Android:

"apk" or "appbundle"

For iOS:

"ipa"
1
  • 1
    a more example, this work for me: flutter build apk --release --no-sound-null-safety
    – ege men
    Commented Nov 5, 2021 at 0:15
6
  1. Execute the following command in the terminal to accept all SDK package licenses

    flutter doctor --android-licenses
    
  2. Run the following command in the terminal to check if there are any platform dependencies to complete the set up:

    flutter doctor
    

    OUTPUT:

    Doctor summary (to see all details, run flutter doctor -v):

    [√] Flutter (Channel dev, 2.2.0-10.1.pre, on Microsoft Windows [Version 10.0.19042.928], locale en-US)

    [√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)

    [√] Chrome - develop for the web

    [√] Android Studio (version 4.1.0)

    [√] Visual Studio Code (version 1.55.2)

    [√] Connected device (3 available)

    • No issues found!

  3. If no issues are found then execute the following command to build an application with unsound null safety

    flutter run --no-sound-null-safety
    
0
3

Adding to creativecreatorormaybenot's answer:

If you're building your APK file or AAB file without sound null safety:

Just do this on your terminal

flutter build apk --split-per-abi --no-sound-null-safety

or

flutter build apk --release --no-sound-null-safety
1

Open a terminal → use this command → flutter run -d chrome --no-sound-null-safety.

This should work.

1

Suppose, in case, anyone gets this error for flutter_html: ^0.8.2.

Add the following to your pubspec.yaml file:

dependencies:
  flutter_html: ^3.0.0-alpha.2

So, it is proved that using any dependency in the project must be the latest version which includes null-safety mechanism.

So, before using "--no-sound-null-safety" solution, try to search and use the upgraded version of your dependencies.

1

For visual studio code user, add below to settings.json

"dart.flutterRunAdditionalArgs": [
    "--no-sound-null-safety"
],
0
1

use this one its worked for me

  flutter pub upgrade --null-safety
1
  • 1
    command not supposed now - The --null-safety flag is no longer supported. Commented Dec 11, 2023 at 7:47
0

I would recommend you all install this package, to maintain the control over the null values in other implementations:

https://pub.dev/packages/flutter_swiper_null_safety/install

-1

Update your library version to the latest. Nowadays most of the library support.

1
  • Re "Nowadays most of the library support": Something seems to be missing. Do you mean "Nowadays, most of the libraries support it"? Or something else? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today). Commented Aug 19, 2022 at 13:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.