启动画面上的启动逻辑永远循环

问题描述

我正在使用启动画面来根据用户登录状态确定向用户发送的路由。

我的服务在 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() 中。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...