Flutter 在 Material 应用程序之间切换或重新加载主函数的方法
我希望在用户注册流程和应用程序的其余部分之间明确分离关注点。为此,我创建了一个用于注册流程的 MaterialApp 和一个用于主应用程序的 MaterialApp。当用户完成注册后 - 他们应该被定向到主应用程序。像这样的事情:
void main() async {
// ... init and stuff ... //
if(isUserAccountExists){
runApp(MultiProvider(
providers: [
Provider(create: (context) => DataStorage()),
ChangeNotifierProvider.value(value: settingsController),
ChangeNotifierProvider.value(value: oauthProvider),
ChangeNotifierProvider.value(value: deviceProvider),
ChangeNotifierProvider.value(value: messenger),
],
child: MainApp(settingsController: settingsController),
),);
} else runApp(EnrollApp());
}
我的问题是当用户完成注册后,我无法重新启动应用程序或重新加载 main()
函数。 (由于 runApp
调用之前的初始化步骤,我必须重新启动)。
I would like to have a clear separation of concerns between user enroll flow and the rest of the app. To do that I have created a MaterialApp for Enroll flow and a MaterialApp for the main application. When the user is done signing up - they should be directed to the main app. So something like this:
void main() async {
// ... init and stuff ... //
if(isUserAccountExists){
runApp(MultiProvider(
providers: [
Provider(create: (context) => DataStorage()),
ChangeNotifierProvider.value(value: settingsController),
ChangeNotifierProvider.value(value: oauthProvider),
ChangeNotifierProvider.value(value: deviceProvider),
ChangeNotifierProvider.value(value: messenger),
],
child: MainApp(settingsController: settingsController),
),);
} else runApp(EnrollApp());
}
My problem is when the user is done signing up, I have no ways to restart the application or reload the main()
function. (I must restart due to initialization steps that precede the runApp
call).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 flutter_phoenix 重新启动您的应用程序。
flutter_phoenix 配置:
您可以按照以下步骤操作:
Phoenix.rebirth(context);
方法重新启动应用程序You can use flutter_phoenix to restart your application.
flutter_phoenix configuration:
You may follow these steps:
Phoenix.rebirth(context);
method to restart the application您可以在单独的
class
(小部件)中提取注册逻辑以根据注册状态呈现哪个屏幕。类似这里:它不需要是
statefulwidget
,您可以使用提供商
或BLOC
模式来处理注册逻辑并更新状态(和UI)因此。You can extract your signup logic in separate
Class
(Widget) to handle which screen to render based on signup status. Something like here:It doesn't needs to be a
StatefulWidget
, you can useProvider
orBloc
pattern to handle signup logic and update your state (and ui) accordingly.