Home > other >  Flutter Bloc Stream Listen not listening to state change
Flutter Bloc Stream Listen not listening to state change

Time:06-03

I have two blocs ( WorkOrderBloc and FilterBottomSheetBloc ) that need to communicate with each other.

MultiBloc Provider I configured a MultiBloc Provider and I am passing the WorkOrderBloc to FilterSheetBottomBloc through its constructor.

@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,
          )),
    );
  }
}
 

In the FilterBottomSheeBloc, I subscribed to the WorkOrderBloc stream and listen for changes emitted from the 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 Both the emitted WorkOrderStatus.loading and WorkOrderStatus.success fine and I can track the changes using the onChange function, but for some reason the workOrderBloc.stream.listen in the FilterBottomSheetBloc isn't responding to the changes from 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'));
      }
    });

Any Suggestions as to why this isn't working ?

CodePudding user response:

Bloc classes are "lazy" by default. It means that they won't be initialised until you use them. So you should use at least one BlocBuilder to get them initialised, or simply use lazy: false parameter when providing a bloc class.

BlocProvider<WorkOrderBloc>(
  create: (context) => WorkOrderBloc(),
  lazy: false,
),
  • Related