Flutter how to programmatically exit the app

后端 未结 7 1308
死守一世寂寞
死守一世寂寞 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.

提交回复
热议问题