Cannot run with sound null safety because dependencies don't support null safety

FlutterDartDart Null-Safety

Flutter Problem Overview


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?

Flutter Solutions


Solution 1 - Flutter

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.

Solution 2 - Flutter

In Android Studio:

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

Enter image description here

Solution 3 - Flutter

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"
                            ]
                }
        ]
}

Solution 4 - Flutter

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.

Solution 5 - Flutter

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
    

Solution 6 - Flutter

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

Solution 7 - Flutter

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...

Solution 8 - Flutter

  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
    

Solution 9 - Flutter

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.

Solution 10 - Flutter

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"

Solution 11 - Flutter

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

Solution 12 - Flutter

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

This should work.

Solution 13 - Flutter

Update your library version to the latest. now a days most of the library support.

Solution 14 - Flutter

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 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 upgrade version of your dependencies.

Solution 15 - Flutter

For VSCode user, add below to settings.json

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncreativecreatorormaybenotView Question on Stackoverflow
Solution 1 - FluttercreativecreatorormaybenotView Answer on Stackoverflow
Solution 2 - FlutterissacView Answer on Stackoverflow
Solution 3 - FlutterErfan EghterafiView Answer on Stackoverflow
Solution 4 - Flutterbalu kView Answer on Stackoverflow
Solution 5 - FlutterCopsOnRoadView Answer on Stackoverflow
Solution 6 - FlutterYanniView Answer on Stackoverflow
Solution 7 - FlutterOm PandeYView Answer on Stackoverflow
Solution 8 - FlutterKavya SView Answer on Stackoverflow
Solution 9 - FlutterAyush SthView Answer on Stackoverflow
Solution 10 - FlutterABDERRAHMANE OUALIView Answer on Stackoverflow
Solution 11 - FlutterSambhav jainView Answer on Stackoverflow
Solution 12 - FlutterMohamed MansourView Answer on Stackoverflow
Solution 13 - FlutterMd Ariful IslamView Answer on Stackoverflow
Solution 14 - FlutterNoor HossainView Answer on Stackoverflow
Solution 15 - FlutterEllix4uView Answer on Stackoverflow