首页 > 解决方案 > 启动画面上的启动逻辑永远循环

问题描述

我正在使用初始屏幕来确定根据用户的登录状态向用户发送哪个路由。

我的服务检查 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!

将非常感谢您的帮助

标签: flutterdartstacked

解决方案


build 方法中的任何内容都可以构建多次,例如更新状态时。通过获取模型,您可能正在更新状态,这会触发函数,从而更新状态。你应该把它放在一个initState().


推荐阅读