【问题标题】:Bloc Listener : access to the previous stateBloc Listener:访问前一个状态
【发布时间】:2021-06-22 14:54:37
【问题描述】:

在我的BlocListener 中有一行代码,我只想在满足前一个状态的条件时运行。那么是否可以在listener 中访问之前的状态,我知道我可以在listenWhen 参数中访问它。

BlocListener<AuthenticationBloc, AuthenticationState>(
            listener: (context, state) {
              switch (state.status) {
                case AuthenticationStatus.authenticated:

                  context.read<GetOffersCubit>().getOffersInMyArea();
                  context.read<GetRequestsCubit>().getRequestsInMyArea();
                  context.read<GetFavoritesCubit>().getFavorites();
                  PushNotificationsManager instanceNotificationManager = PushNotificationsManager();
                  instanceNotificationManager.init(state.user.id);

                  // this line should run only if previous.status != AuthenticationStatus.authenticated
                  context.read<NavigatorBloc>().add(GoToMainFrameScreen());

【问题讨论】:

    标签: flutter dart bloc


    【解决方案1】:

    您可以在每次更改时将状态添加到列表中,然后从列表中访问以前的状态。

    我当然不能测试这个,因为我没有你的完整代码,但这样的东西应该可以工作。

     List statusList = [];
     BlocListener<AuthenticationBloc, AuthenticationState>(
                listener: (context, state) {
                  statusList.add(state.status); // adding to list before hitting switch/case
                  switch (state.status) {
                    case AuthenticationStatus.authenticated:
    
                      context.read<GetOffersCubit>().getOffersInMyArea();
                      context.read<GetRequestsCubit>().getRequestsInMyArea();
                      context.read<GetFavoritesCubit>().getFavorites();
                      PushNotificationsManager instanceNotificationManager = PushNotificationsManager();
                      instanceNotificationManager.init(state.user.id);
                    
                      final previousStatusIndex = statusList.length - 1;
                      final previousStatus = statusList[previousStatusIndex];
    
              // this line should run only if previous.status != AuthenticationStatus.authenticated
    
                      if (previousStatus != AuthenticationStatus.authenticated) {
                     
                      context.read<NavigatorBloc>().add(GoToMainFrameScreen());
                        }
    

    【讨论】:

    • 但是我不应该将状态添加到列表之后 switch case 吗?因为否则statusList[previousStateIndex] 将代表当前状态,你不觉得吗?
    • 好吧,正如我所说我无法测试它,但是每次状态更改时都会调用监听器,所以我想当你进入监听器方法时,statusList[statusList.length]将是当前状态,statusList[previousStatusIndex] 将是前一个状态。但这很容易在调试器中检查,如果不是这种情况,也很容易修复。
    • 是的,通过将它放在开关盒之后,它产生了所需的行为。现在一切正常,谢谢!!
    【解决方案2】:

    您要求的功能是内置的。 Bloc documentation

    BlocListener<BlocA, BlocAState>(
        listenWhen: (previousState, currentState) {
            //return true to invoke listener function
            return true;  
        },
        listener: (context, state) {
            // do stuff here based on BlocA's state
        },
        child: Container(),
    )
    

    【讨论】:

      猜你喜欢
      • 2021-02-05
      • 2020-05-21
      • 2021-07-22
      • 2020-05-06
      • 2021-12-09
      • 2021-02-12
      • 2021-03-19
      • 2018-02-01
      • 2022-08-11
      相关资源
      最近更新 更多