flutter - 启动画面上的启动逻辑永远循环
问题描述
我正在使用初始屏幕来确定根据用户的登录状态向用户发送哪个路由。
我的服务检查 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!
将非常感谢您的帮助
解决方案
build 方法中的任何内容都可以构建多次,例如更新状态时。通过获取模型,您可能正在更新状态,这会触发函数,从而更新状态。你应该把它放在一个initState()
.
推荐阅读
- python - 使用 SWIG 使用 3rd-Party 驱动程序构建 Python 模块
- mysql - 从源代码编译 MySQL 共享对象
- javascript - jQuery post 使用 express 接收数据
- awk - 如果特定列匹配,则使用 BASH 将它们添加到同一行
- python - 在 Geopandas 的特定距离内过滤和合并两个数据框中的点
- javascript - 在单击引导下拉列表时对数组中的 AZ 和 ZA 进行排序
- unity3d - Unity3D BezierCurve - 跟随 AI 的 Spline Walker?
- c++ - 手动创建二进制文件 C++
- java - IDEA 编译器错误 Unknown version '8', no version setting will be given to the compiler
- python-3.x - 尝试使用 Homebrew 更新 Python 3