Flutter how to programmatically exit the app

后端 未结 7 1264
死守一世寂寞
死守一世寂寞 2020-11-27 03:28

How can I programmatically close a Flutter application. I\'ve tried popping the only screen but that results in a black screen.

相关标签:
7条回答
  • 2020-11-27 03:40

    Answers are provided already but please don't just copy paste those into your code base without knowing what you are doing:

    If you use SystemChannels.platform.invokeMethod('SystemNavigator.pop'); note that doc is clearly mentioning:

    Instructs the system navigator to remove this activity from the stack and return to the previous activity.

    On iOS, calls to this method are ignored because Apple's human interface guidelines state that applications should not exit themselves.

    You can use exit(0). And that will terminate the Dart VM process immediately with the given exit code. But remember that doc says:

    This does not wait for any asynchronous operations to terminate. Using exit is therefore very likely to lose data.

    Anyway the doc also did note SystemChannels.platform.invokeMethod('SystemNavigator.pop');:

    This method should be preferred over calling dart:io's exit method, as the latter may cause the underlying platform to act as if the application had crashed.

    So, keep remember what you are doing.

    0 讨论(0)
  • 2020-11-27 03:40

    I prefer using

     Future.delayed(const Duration(milliseconds: 1000), () {
            SystemChannels.platform.invokeMethod('SystemNavigator.pop');
          });
    

    Although, exit(0) also works but the app closes abruptly and doesn't look nice, kind of seems that the app has crashed.

     Future.delayed(const Duration(milliseconds: 1000), () {
           exit(0);
          });
    
    0 讨论(0)
  • 2020-11-27 03:42

    Below worked perfectly with me in both Android and iOS, I used exit(0) from dart:io

    import 'dart:io';
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text(widget.title),
          ),
          body: new ... (...),
              floatingActionButton: new FloatingActionButton(
                onPressed: ()=> exit(0),
                tooltip: 'Close app',
                child: new Icon(Icons.close),
              ), 
        );
      }
    

    UPDATE Jan 2019 Preferable solution is:

    SystemChannels.platform.invokeMethod('SystemNavigator.pop');
    

    As described here

    0 讨论(0)
  • 2020-11-27 03:44

    You can do this with SystemNavigator.pop().

    0 讨论(0)
  • 2020-11-27 03:49

    For iOS

    SystemNavigator.pop(): Does NOT WORK

    exit(0): Works but Apple may SUSPEND YOUR APP because it's against Apple Human Interface guidelines to exit the app programmatically.


    For Android

    SystemNavigator.pop(): Works and is the RECOMMENDED way of exiting the app.

    exit(0): Also works but it's NOT RECOMMENDED as it terminates the Dart VM process immediately and user may think that the app just got crashed.

    0 讨论(0)
  • 2020-11-27 03:59

    Below worked perfectly with me in both Android and iOS import 'dart:io';

      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text(widget.title),
          ),
          body: new ... (...),
              floatingActionButton: new FloatingActionButton(
                onPressed: ()=> SystemNavigator.pop(),
                tooltip: 'Close app',
                child: new Icon(Icons.close),
              ), 
        );
      }
    
    0 讨论(0)
提交回复
热议问题