Flutter: how to prevent device orientation changes and force portrait?

前端 未结 18 1449
鱼传尺愫
鱼传尺愫 2020-12-04 05:41

I would like to prevent my application from changing its orientation and force the layout to stick to \"portrait\".

In the main.dart, I put:

void mai         


        
18条回答
  •  悲哀的现实
    2020-12-04 06:16

    The 'setPreferredOrientations' method returns a Future object. Per documentation a Future represents some value that will be available somewhere in future. That's why you shall wait until it's available and then move on with the application. Hence, there shall be used 'then' method, which, per definition, "registers callbacks to be called when the Future completes". Hence, you shall use this code:

      void main() {
      SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) {
          runApp(new App());
        });
       }
    

    Also, the following file must be imported:

    'package:flutter/services.dart'

提交回复
热议问题