Flutter Bloc:为什么在通知父BlocListener之前以新状态构建子BlocBuilder?

时间:2020-11-02 01:29:45

标签: flutter bloc flutter-bloc

我有一个根级别BlocListener,它监听全局AuthBloc的身份验证状态更改,并在用户退出时使用Navigator返回登录页面。

如果HomePageAuthBloc位于AuthenticatedState中,则后代HomePage小部件也会消耗该auth状态。但是,退出后,子UnauthenticatedState会使用新的BlocListener构建,而根class App extends StatelessWidget { @override Widget build(BuildContext context) { // assume AuthBloc has already been provided return BlocBuilder<AuthBloc, AuthState>( listener: (context, state) { if (state is! AuthenticatedState) { Navigator.pushNamed(context, '/login'); // ^--- I expect this to be called before HomePage gets UnauthenticatedState } }, child: HomePage(), ); } } class HomePage extends StatelessWidget { @override Widget build(BuildContext) { return BlocBuilder<AuthBloc, AuthState>( builder: (context, state) { // state == Unauthenticated after signing out <------ } ); } } 才有机会导航回登录名。

parent->child

这是预期的行为,还是应该有保证的args_mkdir = 'echo '+admin.pw+' | su -c "mkdir -p '+dst4+' && chmod 755 '+dst4+' && cp -r '+\ source_file+' '+dst4+' && echo "'+comment+'" > '+dst4+'/readme_{}.txt'.format(getpass.getuser())+'" '+admin.name try: subprocess.call(args_mkdir, shell=True) except Exception, e: print(e) sys.exit(1) else: mail(comment) 更新顺序?建议的处理方式是什么?

1 个答案:

答案 0 :(得分:0)

在首页的BlocBuilder中

添加条件

class HomePage extends StatelessWidget {
 @override

 Widget build(BuildContext) {
  return BlocBuilder<AuthBloc, AuthState>(
    condition: (AuthState previousState, AuthState currentState) => currentState is AuthenticatedState,
    builder: (context, state) {
     /// Build your home page screen here
    }
  );
}

}