Flutter 从其他 Bloc 监听 Bloc 状态

Lor*_*rti 9 flutter flutter-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();
  }
Run Code Online (Sandbox Code Playgroud)

现在我有这个问题:在调试时我试图打印 stateAuth 它返回:

stateAuth = {AuthenticationAuthenticated} AuthenticationAuthenticated
   props = {_ImmutableList} size = 0
Run Code Online (Sandbox Code Playgroud)

但是stateAuth 是 AuthenticationAuthenticated返回始终为 false。

有没有办法从其他 Bloc 类中监听 blocState?

小智 15

要回答 Sampir 的问题,是的,你是对的,但有时你可能想以另一种方式来做。集团是为其他人管理事件的东西。如果您使用 ui 事件,您的 bloc 会为您的 ui 管理它们,但是如果您还使用其他类型的事件(即位置事件或其他流事件),您可以拥有一个管理您的 ui 事件和其他 bloc 的 bloc管理其他类型的事件(即蓝牙连接)。所以第一个块必须听第二个(即因为正在等待建立蓝牙连接)。考虑一个使用大量传感器的应用程序,每个传感器都有自己的数据流,您将拥有一系列必须合作的区块。

您几乎可以在任何地方向一个块添加一个侦听器。使用 StreamSubscription,您可以为每一种流添加一个侦听器,甚至是另一个块中的流。集团必须有一个方法来公开他的流,所以你可以听他的。

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

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
}
Run Code Online (Sandbox Code Playgroud)


God*_*ias 14

您可能不希望您的块依赖于具有直接块到块依赖关系的另一个块,相反,您可能希望通过表示层连接您的块。

您可以使用 BlocListener 侦听一个块,并在第一个块发生更改时将事件添加到另一个块。

class MyWidget extends StatelessWidget {
  const MyWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocListener<WeatherCubit, WeatherState>(
      listener: (context, state) {
        // When the first bloc's state changes, this will be called.
        //
        // Now we can add an event to the second bloc without it having
        // to know about the first bloc.
        BlocProvider.of<SecondBloc>(context).add(SecondBlocEvent());
      },
      child: TextButton(
        child: const Text('Hello'),
        onPressed: () {
          BlocProvider.of<FirstBloc>(context).add(FirstBlocEvent());
        },
      ),
    );
  }
}

Run Code Online (Sandbox Code Playgroud)

上面的代码阻止 SecondBloc 需要了解 FirstBloc,从而鼓励松散耦合。

查看官方文档以获取更多信息。

  • 这应该是正确的答案,其他答案已经过时了。 (3认同)

geo*_*ngs 12

实际上,在bloc 库的一个示例中,他们从另一个 Bloc (FilteredTodosBloc) 收听 Bloc (TodosBloc)。

class FilteredTodosBloc extends Bloc<FilteredTodosEvent, FilteredTodosState> {
  final TodosBloc todosBloc;
  StreamSubscription todosSubscription;

  FilteredTodosBloc({@required this.todosBloc}) {
    todosSubscription = todosBloc.listen((state) {
      if (state is TodosLoadSuccess) {
        add(TodosUpdated((todosBloc.state as TodosLoadSuccess).todos));
      }
    });
  }
...
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看此示例的说明。

  • 文章被标记为过时,最新已经不再使用这种方式了,不知道是否还合适。 (8认同)

Vin*_*oft 8

最近更新了 bloc 源代码,需要对解决方案进行一些小的更改。

您现在必须收听 bloc/cubit 的流属性,请参阅下面的示例。

class FilteredTodosBloc extends Bloc<FilteredTodosEvent, FilteredTodosState> {
  final TodosBloc todosBloc;
  StreamSubscription todosSubscription;

  FilteredTodosBloc({@required this.todosBloc}) {
    todosSubscription = todosBloc.stream.listen((state) {
    //                             ^^^^^
      if (state is TodosLoadSuccess) {
        add(TodosUpdated((todosBloc.state as TodosLoadSuccess).todos));
      }
    });
  }
...
Run Code Online (Sandbox Code Playgroud)