简体   繁体   English

如何在颤动中使用 bloc 更新小部件的状态?

[英]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.我在屏幕上有 2 个小部件 - A 和 B。在小部件 B 上执行了一个事件,在该事件上 A 和 B 的状态应该得到更新。 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.我为它们中的每一个使用了不同的块和状态,并使用 Futures 的方法在加载屏幕时从 api 获取数据。
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.在小部件 B 的事件中,如何在不再次调用 api 的情况下更新小部件 A 和 B 的状态,因为我可以从上一个会话以及小部件 B 上的事件响应中获取数据,可以构建UI 本身的状态。

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使用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();
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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