212

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402

17 Answers17

341

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
  • 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"). – creativecreatorormaybenot Mar 13 '21 at 15:35
  • 1
    it it possible add fastlane build with `no-sound-null-safety` parameter? @creativecreatorormaybenot – Dolphin Mar 14 '21 at 08:33
  • not works in flutter 2.2.x when build a ios package @creativecreatorormaybenot https://stackoverflow.com/questions/67914266/seems-no-sound-null-safety-parameter-not-work-in-flutter-2-2-1 – Dolphin Jun 13 '21 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 Sep 22 '21 at 01:51
155

In Android Studio:

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

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
issac
  • 1,681
  • 1
  • 8
  • 3
  • in flutter 2.2.x when build ios it works? @issac https://stackoverflow.com/questions/67914266/seems-no-sound-null-safety-parameter-not-work-in-flutter-2-2-1 – Dolphin Jun 13 '21 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 – Trophy Developers Sep 05 '21 at 19:44
58

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.

balu k
  • 3,515
  • 2
  • 14
  • 29
54

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
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
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"
                            ]
                }
        ]
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Erfan Eghterafi
  • 4,344
  • 1
  • 33
  • 44
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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yanni
  • 580
  • 2
  • 6
  • 21
  • 2
    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 Oct 04 '21 at 08: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...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Om PandeY
  • 91
  • 2
  • 2
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ayush Sth
  • 317
  • 6
  • 11
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"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kavya S
  • 511
  • 1
  • 5
  • 9
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sambhav jain
  • 1,467
  • 12
  • 19
2

use this one its worked for me

  flutter pub upgrade --null-safety
Rasathurai Karan
  • 673
  • 5
  • 16
1

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

This should work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohamed Mansour
  • 197
  • 2
  • 4
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Noor Hossain
  • 1,620
  • 1
  • 18
  • 25
1

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

"dart.flutterRunAdditionalArgs": [
    "--no-sound-null-safety"
],
Ellix4u
  • 566
  • 5
  • 9
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 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](https://stackoverflow.com/posts/70727651/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 19 '22 at 13:39