简体   繁体   English

如何从 main() 的开头重新启动 Flutter 应用程序?

[英]How to restart a Flutter app from the beginning of the main()?

I want to restart my app at some point from the beginning of the main() function and not just rebuild the whole widget tree.我想在main() function 开头的某个时间点重新启动我的应用程序,而不仅仅是重建整个小部件树。

Here is an example of my code, the thing is I want to call init() when restart这是我的代码示例,问题是我想在重启时调用 init()

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await init();
  runApp(
    const MyApp(),
  );
}

I have tried to wrap the widget tree with a Restart widget and tried phoenix package我试图用 Restart 小部件包装小部件树并尝试了phoenix package

But both of them just rebuild the widget tree regardless of what is above in main().但是无论 main() 上面的内容如何,它们都只是重建了小部件树。 I even tried to call the main() directly but it didn't work as expected.我什至尝试直接调用 main() 但它没有按预期工作。

Is there any solution for this case?这种情况有什么解决办法吗?

I think you can try "Restart App" module我认为您可以尝试“重新启动应用程序”模块

  1. Instal from your teminal:从您的终端安装:

flutter pub add restart_app

  1. Import from your dart file:从您的 dart 文件导入:

import 'package:restart_app/restart_app.dart';

  1. Call "Restart App" from main():从 main() 调用“重启应用程序”:

onPressed: () { Restart.restartApp();} onPressed: () { Restart.restartApp();}

Have a nice day...祝你今天过得愉快...

You runApp(YourApp()) again where you want to restart.在要重新启动的地方再次运行 App(YourApp())。 1 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM