实际使用案例:
>在初始化过程中,应用程序检测到没有网络连接.缺乏网络连接可能阻止了正确的启动(例如加载外部资源,如JSON文件……).
>在初始握手期间,需要下载一些重要资源的新版本(更新类型).
在这两种用例中,我希望应用程序继续完全重启,而不是必须在ApplicationState级别构建复杂的逻辑.
非常感谢您的提示.
解决方法 您可以将整个应用程序包装到statefulWidget中.当您想重新启动应用程序时,请使用具有不同密钥的子项重建该statefulWidget.这会让你失去你的应用程序的整个状态.
import 'package:Flutter/material.dart';voID main() { runApp(new RestartWidget( child: // new MaterialApp,));}class RestartWidget extends StatefulWidget { final Widget child; RestartWidget({this.child}); static restartApp(BuildContext context) { final _RestartWidgetState state = context.ancestorStateOfType(const TypeMatcher<_RestartWidgetState>()); state.restartApp(); } @overrIDe _RestartWidgetState createState() => new _RestartWidgetState();}class _RestartWidgetState extends State<RestartWidget> { Key key = new UniqueKey(); voID restartApp() { this.setState(() { key = new UniqueKey(); }); } @overrIDe Widget build(BuildContext context) { return new Container( key: key,child: Widget.child,); }}
在此示例中,您可以使用RestartWidget.restartApp(context)从任何位置重置您的应用程序.
总结以上是内存溢出为你收集整理的颤动:如何强制重启应用程序(在生产模式下)?全部内容,希望文章能够帮你解决颤动:如何强制重启应用程序(在生产模式下)?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)