我正在使用启动画面来根据用户的登录状态确定向用户发送的路由。
我的服务在 firebase 上检查用户状态,如下所示:
Stream<User?> isUserLoggedIn() {
var user = _firebaseAuth.authStateChanges();
return user;
}
我的模型调用服务并根据响应确定用户路由:
User? handleStartUpLogic() {
print('Run only once and stop magically printing text!');
_authenticationServices.isUserLoggedIn().listen((User? user) {
if (user == null) {
print('User is signed out!');
goToSignIn();
} else {
print('User is signed in!');
goToMarket();
}
});
}
...在我的视图中,我设置了模型就绪:
return ViewModelBuilder<SplashModel>.reactive(
onModelReady: (model) => model.handleStartUpLogic(),
createNewModelOnInsert: false,
builder: (context, model, child) => Scaffold(
backgroundColor: fontColor,
body: Center(child: Image.asset('assets/fable_Icon.png')),
),
当我运行这段代码时,我得到了这个无休止的循环:
I/flutter (12991): Run only once and stop magically printing text!
I/flutter (12991): Run only once and stop magically printing text!
I/chatty (12991): uid=10080(u0_a80) 1.ui identical 10 lines
I/flutter (12991): Run only once and stop magically printing text!
I/flutter (12991): Run only once and stop magically printing text!
I/flutter (12991): Run only once and stop magically printing text!
I/flutter (12991): Run only once and stop magically printing text!
I/chatty (12991): uid=10080(u0_a80) 1.ui identical 32 lines
I/flutter (12991): Run only once and stop magically printing text!
I/zygote (12991): Do partial code cache collection, code=60KB, data=43KB
I/zygote (12991): After code cache collection, code=57KB, data=42KB
I/zygote (12991): Increasing code cache capacity to 256KB
I/flutter (12991): Run only once and stop magically printing text!
I/flutter (12991): Run only once and stop magically printing text!
I/chatty (12991): uid=10080(u0_a80) 1.ui identical 156 lines
I/flutter (12991): Run only once and stop magically printing text!
I/flutter (12991): Run only once and stop magically printing text!
I/chatty (12991): uid=10080(u0_a80) 1.ui identical 15 lines
I/flutter (12991): Run only once and stop magically printing text!
I/flutter (12991): Run only once and stop magically printing text!
I/flutter (12991): Run only once and stop magically printing text!
I/chatty (12991): uid=10080(u0_a80) 1.ui identical 54 lines
I/flutter (12991): Run only once and stop magically printing text!
I/flutter (12991): Run only once and stop magically printing text!
I/chatty (12991): uid=10080(u0_a80) 1.ui identical 21 lines
I/flutter (12991): Run only once and stop magically printing text!
I/flutter (12991): Run only once and stop magically printing text!
非常感谢您的帮助
答案 0 :(得分:0)
build 方法中的任何内容都可以多次构建,例如更新状态时。潜在地,通过获取模型,您正在更新状态,从而触发功能,从而更新状态。你应该把它放在一个 initState()
中。