【发布时间】:2020-03-26 23:07:06
【问题描述】:
您好,我正在尝试从其他 bloc 中侦听 bloc 的状态。 我正在使用这个包https://pub.dev/packages/bloc
从我的 UserBloc 我想听 AuthBloc 并且当它具有状态 AuthenticationAuthenticated 时 UserBloc 应该触发事件。
final UserRepository userRepository;
final authBloc;
StreamSubscription authSub;
UserBloc({ @required this.userRepository, @required this.authBloc}) {
authSub = authBloc.listen((stateAuth) {
//here is my problem because stateAuth, even is AuthenticationAuthenticated it return always false.
if (stateAuth is AuthenticationAuthenticated) {
this.add(GetUser()) ;
}
});
}
@override
Future<void> close() async {
authSub?.cancel();
super.close();
}
现在我有这个问题: 在调试时我试图打印 stateAuth 它返回:
stateAuth = {AuthenticationAuthenticated} AuthenticationAuthenticated
props = {_ImmutableList} size = 0
但 stateAuth 是 AuthenticationAuthenticated 总是返回 false。
有什么方法可以从其他 Bloc 类中监听 blocState 吗?
【问题讨论】:
-
你做到了吗,我正在尝试完成类似的事情。
标签: flutter flutter-bloc