如何以编程方式关闭Flutter应用程序。我尝试弹出唯一的屏幕,但是会导致黑屏。
Answers:
SystemNavigator.pop():无效
exit(0):可以,但是Apple可能会暂停您的APP,因为违反Apple Human Interface指南以编程方式退出该应用。
SystemNavigator.pop():有效,并且是退出应用程序的推荐方式。
exit(0):也可以使用,但不建议这样做,因为它会立即终止Dart VM进程,并且用户可能会认为该应用程序刚刚崩溃。
exit(0)根据需要尝试。
                    SystemNavigator.pop()退出我的应用程序并显示崩溃消息,exit(0)按预期方式工作
                    下面我完美地工作在这两个Android和iOS,我用exit(0)从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),
          ), 
    );
  }
2019年1月更新首选的 解决方案是:
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
如上所述这里
t work for me... I试图向CupertinoTabScafold添加退出按钮。使用()=> exit(0),它会显示一个带有标签栏的空白屏幕。
                    您可以使用SystemNavigator.pop()。
exit(0);确实有效
                    SystemNavigator.pop被忽略,因为Apple的人机界面指南指出应用程序不应退出自身。在Android上,它应该可以工作,并且比调用dart:io的exit方法更可取。考虑提出问题:github.com/flutter/flutter/issues/new
                    已经提供了答案,但是请不要在不知道自己在做什么的情况下将其复制粘贴到代码库中:
如果您使用doc明确提到的SystemChannels.platform.invokeMethod('SystemNavigator.pop');注释:
指示系统导航器从堆栈中删除此活动并返回上一个活动。
在iOS上,对此方法的调用将被忽略,因为Apple的人机界面指南指出应用程序不应自行退出。
您可以使用exit(0)。这将立即使用给定的退出代码终止Dart VM进程。但是请记住该文档说:
这不等待任何异步操作终止。因此,使用出口很可能会丢失数据。
无论如何,文档也注意到了SystemChannels.platform.invokeMethod('SystemNavigator.pop');:
与调用dart:io的exit方法相比,此方法应该更为可取,因为后者可能导致基础平台的运行就像应用程序崩溃一样。
因此,请记住您在做什么。
我更喜欢使用
 Future.delayed(const Duration(milliseconds: 1000), () {
        SystemChannels.platform.invokeMethod('SystemNavigator.pop');
      });
尽管exit(0)也可以工作,但应用程序突然关闭并且看起来不太好,似乎该应用程序已崩溃。
 Future.delayed(const Duration(milliseconds: 1000), () {
       exit(0);
      });
    这对我有用;
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),
      ), 
  );
}
    下面在Android和iOS导入'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),
          ), 
    );
  }
    SystemNavigator.pop()在iOS中不起作用,请阅读我的回答。至少到目前为止,还没有办法在iOS应用程序中退出。
                    
SystemNavigator.pop()在Android模拟器下无法正常工作。该应用程序仍在后台运行。因此,我不确定它是否可以在真实设备上正常运行。