简体   繁体   English

返回 Widget 闪屏,flutter-bloc 模式

[英]Flickering screen of returning Widget, flutter-bloc pattern

I'm new to flutter, I just followed the firebase login flutter-bloc tutorial to do the same operation for my app,我是flutter的新手,我只是按照firebase登录flutter-bloc教程为我的应用程序做同样的操作,

Everything goes well, except the first screen loading.一切顺利,除了第一个屏幕加载。

home: BlocBuilder<AuthBloc, AuthState>(
    builder: (context, state) {
      if (state is Uninitialized) { <=== return regardless of the state
        return WelcomeScreen();
      } else if (state is Unauthenticated) { <=== return regardless of the state
        return LoginScreen(userRepository: _userRepository);
      } else if (state is Authenticated) {
        return HomeScreen( <=== return regardless of the state
          user: state.user,
          homeRepository: _homeRepository,
          userRepository: _userRepository,
        );
      }
      return WelcomeScreen();
    },

flutter-bloc main.dart 颤振块 main.dart

The if condition return every screen regardless of the state, if state condition met, It should break the condition and return only one widget. if 条件返回每个屏幕而不考虑状态,如果满足状态条件,它应该打破条件并只返回一个小部件。 But it's not the case here, it returns every screen weird.但这里不是这样,它返回的每个屏幕都很奇怪。

加载屏幕后的颤振杂耍

Thanks谢谢

A likely cause on why the screen flickers is that a new screen is being returned by BlocBuilder() .屏幕闪烁的一个可能原因是BlocBuilder()正在返回一个新屏幕。 Per docs, BlocBuilder rebuilds in response to new states.根据文档, BlocBuilder会重建以响应新状态。 To prevent unnecessary rebuilds you're not expecting, you may consider using BlocListener as it's guaranteed to be called only once for each state change unlike the builder in BlocBuilder .为了防止不必要的重建,您可以考虑使用BlocListener ,因为与BlocBuilder中的builder不同,它保证每次state更改只调用一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM