Flutter从其他Bloc监听Bloc状态 [英] Flutter listen Bloc state from Other Bloc

查看:102
本文介绍了Flutter从其他Bloc监听Bloc状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在尝试从其他集团听集团的状态.我正在使用此软件包 https://pub.dev/packages/bloc

Hello I'm trying to listen state of bloc form other bloc. I'm using this package https://pub.dev/packages/bloc

我要从我的 UserBloc 中收听 AuthBloc ,并且当其状态为 AuthenticationAuthenticated 时, UserBloc 应该触发事件.

From my UserBloc I want listen AuthBloc and when It has the state AuthenticationAuthenticated the UserBloc should fire an event.

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时返回:

For now I have this problem: When in debug I'm trying to print stateAuth it return:

stateAuth = {AuthenticationAuthenticated} AuthenticationAuthenticated
   props = {_ImmutableList} size = 0

但是 stateAuth是AuthenticationAuthenticated 总是返回false.

But stateAuth is AuthenticationAuthenticated return always false.

有什么方法可以从其他Bloc类中监听blocState吗?

Is there any way for listen blocState From Other Bloc class?

推荐答案

要回答Sampir的问题,是的,您是对的,但有时您可能想用另一种方式来做.团体是为其他人管理事件的东西.如果您正在使用ui事件,则您的集团将为您的ui管理它们,但是如果您还使用其他类型的事件(例如,位置事件或其他流事件),则可以拥有一个用于管理ui事件和其他集团的集团管理其他类型的事件(即蓝牙连接).因此,第一个集团必须收听第二个集团(即因为正在等待建立蓝牙连接).考虑一个使用大量传感器的应用程序,每个传感器都有其数据流,您将拥有一系列必须协作的阵​​营.您可以使用多提供者和多侦听器来完成此操作,但是您的链可能会很长,并且编写侦听器案例可能很困难,或者您可能希望将其隐藏在用户界面中,或者希望在其他部分重用它应用程序,因此您可能希望在自己的集团内部建立链.

To answer Sampir's question, yes, you're right, but sometimes you may want to do it in another way. A bloc is something that manages an event for someone else. If you are working with ui events, your bloc manages them for your ui, but if you are working also with other kind of events (i.e. position events, or other streams events) you can have a bloc that manages your ui events and antoher bloc that manages the other kind of events (i.e. a bluetooth connection). So the first bloc must listen to the second one (i.e. because is waiting for establishing bluetooth connection). Think about an app that uses a lot of sensors, each one with its stream of data, and you'll have a chain of blocs that have to cooperate. You can do it with multi-provider and multi-listener but your chain could be very long and writing your listener cases can be hard, or you may want to hide it from your ui, or you want to reuse it in another part of your app, so you may want to build your chain inside your blocs.

您几乎可以在任何地方将侦听器添加到集团中.使用StreamSubscription,您可以为每种流添加侦听器,甚至可以将侦听器添加到另一块中.该集团必须具有公开他的信息流的方法,以便您可以听他讲话.

You can add a listener to a bloc almost everywhere. Using StreamSubscription, you can add a listener to every kind of streams, even the one in another bloc. The bloc must have a method to expose his stream, so you can listen to him.

一些代码(我使用flutter_bloc-flutter_bloc具有多个提供者,但这仅是示例):

Some code (I use flutter_bloc - flutter_bloc has multi-providers, but it's just for example):

class BlocA extends Bloc<EventA, StateA> {

  final BlocB blocB;
  StreamSubscription subscription;

  BlocA({this.blocB}) {
    if (blocB == null) return;
    subscription = blocB.listen((stateB) {
      //here logic based on different children of StateB
    });
  }

  //...

}

class BlocB extends Bloc<EventB, StateB> {
   //here BlocB logic and activities
}

这篇关于Flutter从其他Bloc监听Bloc状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆