Home > Software design >  My Flutter app can't stay portraitUp even though I have added the necessary codes
My Flutter app can't stay portraitUp even though I have added the necessary codes

Time:09-18

 void main() {
  WidgetsFlutterBinding.ensureInitialized;
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  runApp(const MyApp());
}

I add this section to "main.dart" but it didn't work on Samsung Phone.

CodePudding user response:

setPreferredOrientation is async, i think the issue might be that the app run before setPreferredOrientation is finished. Try using the "then" function to run the app after the function is done. Like this :

WidgetsFlutterBinding.ensureInitialized;
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then(
  (value) => runApp(const MyApp()));
  • Related