How to build APK with no sound null safety

41,944

Solution 1

It was only necessary to use additional comment // @dart=2.9 before all imports in main.dart so it could run without any flags

So main.dart should look like this:

// @dart=2.9
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';
import '../init.dart';

Future<void> main() async {
  GestureBinding.instance?.resamplingEnabled = true;
  WidgetsFlutterBinding.ensureInitialized();
  await init();
  runApp(MyApp());
}

Any other .dart files don't require annotating them

P.S. make sure you updated pubspec.yaml for using Dart 2.12:

environment:
  sdk: '>=2.12.0-0.0 <3.0.0'

Solution 2

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 3

we can try this,

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

or by adding in main.dart , 1st Line

 // @dart=2.9

Solution 4

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

Share:
41,944
Andrey Gritsay
Author by

Andrey Gritsay

Updated on July 25, 2022

Comments

  • Andrey Gritsay
    Andrey Gritsay almost 2 years

    I can run an app in release mode on my phone with passing flag --no-sound-null-safety --release, but neither flutter build apk --enable-experiment=non-nullable nor flutter build apk --no-sound-null-safety nor flutter build apk --enable-experiment=non-nullable --no-sound-null-safety will work

  • Void
    Void over 3 years
    It does not fixes the problem, as it only states that the file does not supports null safety.
  • Andrey Gritsay
    Andrey Gritsay over 3 years
    I don'y see any problem as main.dart should be used only as an entry point such as runApp(MyApp()) and actual MyApp() widget could be in another file
  • Void
    Void over 3 years
    But this other file would still have the problem. If it uses null-safety
  • Andrey Gritsay
    Andrey Gritsay over 3 years
    Nope. My full project uses null-safety and only main.dart doesn't and everything works as expected
  • Void
    Void over 3 years
    Are you sure? Because the doc say otherwise : 'Add a language version comment to the top of any Dart files that you don’t want to consider during your current migration:' So, other file would still be checked for null safety
  • Andrey Gritsay
    Andrey Gritsay over 3 years
    Yes, I'm sure because otherwise I wouldn't mark it as an answer. Before annotating flutter build apk produced a lot of errors related to null checks
  • Tin Man
    Tin Man over 3 years
    // @dart=2.9 marks the whole file to be used with Dart 2.9. If only a few of the libraries used as imports are nullable, but the rest of the file has been adjusted for null safety (and uses e.g. the nullability character ?), this won't work.
  • Andrey Gritsay
    Andrey Gritsay over 3 years
    @TinMan Everything works if you put everything out of main.dart and leave it with only runApp() function
  • Dolphin
    Dolphin about 3 years
    it did not works when the flutter version is 2.2.x when build ios pakcage.@Adam Musa
  • Adam Musa
    Adam Musa about 3 years
    In flutter 2.x null safety is enabled by default. So to solve this you have to downgrade the sdk version of dart to 2.7.x. And you change the way of coding when you bring your sdk back to 2.7.x because it does not support the philosophy of sdk> = 2.10.x
  • Rebar
    Rebar over 2 years
    Not working for 2022 with Flutter 2.10.0