26

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

Andrey Gritsay
  • 979
  • 1
  • 10
  • 18

5 Answers5

59

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
Adam Musa
  • 1,252
  • 1
  • 8
  • 9
  • it did not works when the flutter version is 2.2.x when build ios pakcage.@Adam Musa – Dolphin Jun 13 '21 at 11:40
  • 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 – Adam Musa Jun 14 '21 at 10:59
14

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'
Andrey Gritsay
  • 979
  • 1
  • 10
  • 18
  • 1
    It does not fixes the problem, as it only states that the file does not supports null safety. – Void Jan 11 '21 at 22:24
  • 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 – Andrey Gritsay Jan 12 '21 at 09:01
  • But this other file would still have the problem. If it uses null-safety – Void Jan 12 '21 at 13:38
  • Nope. My full project uses null-safety and only `main.dart` doesn't and everything works as expected – Andrey Gritsay Jan 12 '21 at 13:52
  • 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 – Void Jan 12 '21 at 14:54
  • 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 – Andrey Gritsay Jan 12 '21 at 18:39
  • // @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. – Tin Man Jan 26 '21 at 19:53
  • @TinMan Everything works if you put everything out of `main.dart` and leave it with only `runApp()` function – Andrey Gritsay Jan 26 '21 at 19:59
  • Not working for 2022 with Flutter 2.10.0 – Rebar Feb 14 '22 at 19:14
9

we can try this,

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

or by adding in main.dart , 1st Line

 // @dart=2.9
Mrudul Addipalli
  • 574
  • 6
  • 10
2

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

0

If You are facing no sound null safety problem then follow 3 Steps.

  1. // @2.9 in the top and remove the parameter from Const app_name();
  2. 2nd solution is run --no-sound-null-safety in the terminal
  3. Edit your Configure File and write in Additional run args:- --no-sound-null-safety

See these images 1 and 2.

cigien
  • 57,834
  • 11
  • 73
  • 112