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
![Andrey Gritsay](https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=256)
Author by
Andrey Gritsay
Updated on July 25, 2022Comments
-
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 neitherflutter build apk --enable-experiment=non-nullable
norflutter build apk --no-sound-null-safety
norflutter build apk --enable-experiment=non-nullable --no-sound-null-safety
will work -
Void over 3 yearsIt does not fixes the problem, as it only states that the file does not supports null safety.
-
Andrey Gritsay over 3 yearsI don'y see any problem as
main.dart
should be used only as an entry point such asrunApp(MyApp())
and actualMyApp()
widget could be in another file -
Void over 3 yearsBut this other file would still have the problem. If it uses null-safety
-
Andrey Gritsay over 3 yearsNope. My full project uses null-safety and only
main.dart
doesn't and everything works as expected -
Void over 3 yearsAre 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 over 3 yearsYes, 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 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 over 3 years@TinMan Everything works if you put everything out of
main.dart
and leave it with onlyrunApp()
function -
Dolphin about 3 yearsit did not works when the flutter version is 2.2.x when build ios pakcage.@Adam Musa
-
Adam Musa about 3 yearsIn 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 over 2 yearsNot working for 2022 with Flutter 2.10.0