简体   繁体   中英

How to update state of widgets using bloc in flutter?

I have 2 widgets on a screen - A and B. There is an event performed on widget B, on which the state of A and B should get updated. I have used different blocs and states for each of them and used the approach of Futures to get data from api while loading the screen.
On an event of widget B, how can I update state of both the widgets A and B without calling the api again as I can get the data from previous session and also from the response of event on widget B, it is possible to build the state on the UI itself.

Adding code:

    class BlocA extends Bloc<BlocAEvent,BlocAState>
    {
      BlocA():super(InitialState()){
      on<GetData>(_getBlocAData);
              }
            }
            
         void _getBlocAData(GetData event, Emitter<BlocAState>emit) 
           async{
             try {
                List<User> getDataResponse = await 
                DataService().getWidgetAData(
                    event.userid);
                
                emit(BlocALoadedState(BlocAData: getDataResponse));
               }
               catch(e){
                rethrow;
               }}
        
          class InitialState extends BlocAState{}
        
           class BlocALoadedState extends BlocAState{
             final List<User> BlocAData;
             BlocALoadedState({required this.BlocAData});
            }
        
          BlocB:
        
          abstract class BlocBStates {}
        
          class BlocBLoadedState extends BlocBStates{
            List<User> BlocBdata;
            BlocBLoadedState({required this.BlocBdata});
          }
        
          class BlocBAcceptedState extends BlocBStates{
            User user;
            BlocBAcceptedState({required this.user});
          }



Now, BlocB has event which fetches data from a different 
enter code heresource.
 
   class BlocB extends Bloc<BlocBEvent,BlocBState>
    {
      BlocB():super(InitialState()){
        on<GetData>(_getBlocBData);
        on<BlocBevent>(_onClickofaButtoninWidgetB);
      }
    }
    
    void _getBlocBData(GetData event, 
    Emitter<BlocBState>emit) 
    async{
      try {
        List<User> getDataResponse = await 
        DataService().getWidgetBData(
            event.userid);
        
        emit(BlocBLoadedState(BlocBData: getDataResponse));
       }
       catch(e){
        rethrow;
       }}

   void _onClickofaButtoninWidgetB(BlocBevent event, 
   Emitter<BlocBStates>emit) {
   User blocBEventResponse = await 
   DataService().acceptRequest( 
   event.senderId,event.receiverId)
   // From this response, I want to add this entry to bloc A's 
   // state and remove from bloc B's state
   }

use BlocListener

BlocProvider(
  create: BlocA(),
  child: BlocListener<BlocB, StateB>(
    listener: (context, state) {
      if (state is StateBLoading) {
        context.read<BlocA>().add(EventALoading());
      } else if (state is StateBLoaded) {
        context.read<BlocA>().add(EventALoaded(state.someData));
      }
    },
    child: WidgetA();
  );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM