1

I have tried using this code I found on a thread on this site but all that happens is my application loads a white screen. Is there a better way to force portrait view in flutter? Seems like simple thing to set.

void main() async {
  ///
  /// Force the layout to Portrait mode
  ///
  await SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);

  runApp(new MaterialApp(
    debugShowCheckedModeBanner: false,
    home: LoginScreen(),
  ));
}
om-ha
  • 3,102
  • 24
  • 37
Sam Cromer
  • 2,063
  • 10
  • 28
  • 50
  • Is your test device Android or iOS? – Abion47 Jan 20 '20 at 23:56
  • If you are getting a white screen it means you are getting an Exception before the call to `runApp`. What is the exception when you try and debug the app? – MichaelM Jan 20 '20 at 23:56
  • its android.... – Sam Cromer Jan 20 '20 at 23:57
  • ERROR is : E/flutter (27219): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized. – Sam Cromer Jan 20 '20 at 23:59
  • OP @SamCromer is primarily asking: **Is there a better way to force portrait view in flutter?** The white screen bugfix everyone is pointing out is secondary, and you guys are not explaining why this is the fix. Additionally, nobody is pointing out there's a gotcha for forcing orientation via `SystemChrome`. The gotcha is regarding iPad's Multitasking flag "Require FullScreen" to be set to the non-default value true. As indicated in my answer [below](https://stackoverflow.com/a/59832614/10830091). – om-ha Jan 21 '20 at 00:27

3 Answers3

5

Change your code as

void main() {
 WidgetsFlutterBinding.ensureInitialized();
 SystemChrome.setPreferredOrientations(
 [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])
 .then((_) => runApp(new MaterialApp(
         debugShowCheckedModeBanner: false,
         home: LoginScreen(), 
      )));
}

`

Khadga shrestha
  • 1,120
  • 6
  • 11
1

From the exception in the comment:

Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized

You need to call

WidgetsBinding.ensureInitialized();

Before your call to SystemChrome.setPreferredOrientations

MichaelM
  • 5,518
  • 2
  • 30
  • 23
1

Solution

  1. Change your code to this:
void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  ///
  /// Force the layout to Portrait mode
  ///
  await SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);

  runApp(new MaterialApp(
    debugShowCheckedModeBanner: false,
    home: LoginScreen(),
  ));
}
  1. Open your Xcode project and go to Target -> Deployment Info -> Require Full Screen to true. This will reflect in your ios/Runner/Info.plist to have the following value:
    <key>UIRequiresFullScreen</key>
    <true/>

Description

Well there are a couple of points to be addressed here:

  1. You forgot WidgetsFlutterBinding.ensureInitialized(). This IS important since you're "awaiting" on async main method.
  2. According to setPreferredOrientations's documentation, there's a limitation regarding iPad multitasking:

This setting will only be respected on iPad if multitasking is disabled.

To alleviate #2, from the docs:

Should you decide to opt out of multitasking you can do this by setting "Requires full screen" to true in the Xcode Deployment Info.

om-ha
  • 3,102
  • 24
  • 37