Flutter Bloc Stream Listen 不监听状态变化

Flutter Bloc Stream Listen not listening to state change

我有两个集团(WorkOrderBloc 和 FilterBottomSheetBloc)需要相互通信。

MultiBloc 提供商 我配置了一个 MultiBloc Provider,并通过其构造函数将 WorkOrderBloc 传递给 FilterSheetBottomBloc。

@override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<HomeBloc>(
          create: (context) => HomeBloc(
              authenticationBloc: BlocProvider.of<AuthenticationBloc>(context)),
        ),
        BlocProvider<WorkOrderBloc>(
          create: (context) => WorkOrderBloc(),
        ),
        BlocProvider<FilterBottomSheetBloc>(
          create: (context) => FilterBottomSheetBloc(
              workOrderBloc: BlocProvider.of<WorkOrderBloc>(context)),
        ),
      ],
      child: Scaffold(
          appBar: AppBar(
            actions: [
              EndDrawerIcon(),
            ],
            title: Text(_appBarTitle,
                style: Theme.of(context).textTheme.headline5),
            backgroundColor: Theme.of(context).colorScheme.secondary,
            leadingWidth: 0,
            centerTitle: false,
          ),
          endDrawer: HomePageDrawer(),
          body: Container(child: pages.elementAt(_currentIndex)),
          bottomNavigationBar: HomePageBottomNavigation(
            index: _currentIndex,
            onTap: _switchPage,
          )),
    );
  }
}
 

在 FilterBottomSheeBloc 中,我订阅了 WorkOrderBloc 流并监听从 WorkOrderBloc 发出的变化

FilterBottomSheetBloc

class FilterBottomSheetBloc extends Bloc<FilterBottomSheetBlocEvent,FilterBottomSheetBlocState> {

  final WorkOrderBloc workOrderBloc;
  late StreamSubscription workOrderBlocSubscription;

  FilterBottomSheetBloc({required this.workOrderBloc})
      : super(FilterBottomSheetBlocState()) {
    
   workOrderBlocSubscription = workOrderBloc.stream.listen((state) {
      print('--- WorkOrderBloc state changed  ---');
      if (state.status == WorkOrderStatus.success)
        add(GetListOfWorkOrders(state.listOfWorkOrders));

    });


    on<GetListOfWorkOrders>((event, emit) {
      emit(state.copyWith(listOfWorkOrders: event.listOfWorkOrders));
      
    });
  } 

My WorkOrderBloc 发出的 WorkOrderStatus.loadingWorkOrderStatus.success 都很好,我可以使用onChange 函数,但出于某种原因,FilterBottomSheetBloc 中的 workOrderBloc.stream.listen 未响应来自 WorkOrderBloc 的更改。

WorkOrderBloc

class WorkOrderBloc extends Bloc<WorkOrderEvent, WorkOderState> {
  final _workOrderRepository = locator.get<WorkOrderRepository>();
  WorkOrderBloc()
      : super(WorkOderState(
            status: WorkOrderStatus.inital,
            listOfWorkOrders: [],
            filterOptions: FilterOptions(listOfWorkOrderStatusTypes: []))) {
    on<getWorkOrders>((event, emit) async {
      try {
        emit(state.copyWith(status: WorkOrderStatus.loading));
        List<WorkOrder> workOrders = await _workOrderRepository.getWorkOrders();
        emit(state.copyWith(
            status: WorkOrderStatus.success, listOfWorkOrders: workOrders));
      } catch (e) {
        print(e);
        emit(state.copyWith(
            status: WorkOrderStatus.failure,
            failedMessage: 'Could not load your work orders'));
      }
    });

关于为什么这不起作用的任何建议?

Bloc class默认情况下是“懒惰的”。这意味着在您使用它们之前它们不会被初始化。因此,您应该至少使用一个 BlocBuilder 来初始化它们,或者在提供 bloc class.

时简单地使用 lazy: false 参数
BlocProvider<WorkOrderBloc>(
  create: (context) => WorkOrderBloc(),
  lazy: false,
),