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

Viewed 322482

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?

15 Answers

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.

In Android Studio:

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

Enter image description here

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

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.

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
    

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

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

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

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.

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

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

This should work.

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.

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

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

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