flutter 扑动块在之后未重建

vc9ivgsu  于 2023-05-19  发布在  Flutter
关注(0)|答案(1)|浏览(170)

集团不奇怪不更新用户界面,即使我可以看到内部没有更新。当我添加到状态Set<String>时,我确实得到了重建。但在移除时,我没有得到重建。
我的州

part of 'global_loading_bloc.dart';

class GlobalLoadingState extends Equatable {
   GlobalLoadingState({subscriptions})
      : subscriptions = subscriptions ?? {};
  final Set<String> subscriptions;

  GlobalLoadingState copyWith({Set<String>? subscriptions}) {
    return GlobalLoadingState(
      subscriptions: subscriptions ?? this.subscriptions,
    );
  }

  @override
  List<Object?> get props => [subscriptions];
}

集团

import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:equatable/equatable.dart';
import 'package:julie_food/blocs/get_it.dart';
import 'package:julie_food/core/services/models/order.dart';

part 'global_loading_event.dart';
part 'global_loading_state.dart';

class GlobalLoadingBloc extends Bloc<GlobalLoadingEvent, GlobalLoadingState> {
  GlobalLoadingBloc() : super(GlobalLoadingState()) {
    

    on<AddOrder>((event, emit) async {
      // add to Set to look like {'bruh'}
      emit(state.copyWith(subscriptions: state.subscriptions..add(event.key)));
      try {
        await Future.delayed(const Duration(milliseconds: 2000));
        // remove to set to look like {}
        emit(state.copyWith(
            subscriptions: state.subscriptions..remove(event.key)));
        
        event.onSuccess();
        // printing here shows that the state did recieve update
        
      } catch (error) {
        print('delay done ${state.subscriptions}');
        emit(state.copyWith(
            subscriptions: state.subscriptions..remove(event.key)));
      }
    });
  }
}

Widget生成器

BlocBuilder<GlobalLoadingBloc, GlobalLoadingState>(
                    bloc: globalLoadingBloc,
                    builder: (context, state) {
                      print(state.subscriptions); // I dont get a rebuild at remove
                      return ContainerLoadingButton(
                          background: primaryRedColor4,
                          text: 'Order now',
                          loading: state.subscriptions
                              .contains('createOrder-${widget.meal!.mealName}'),
                          onTap: () async {
                            
                            globalLoadingBloc.add(AddOrder(
                                key: 'createOrder-${widget.meal!.mealName}',
                                order: _order!,
                                onSuccess: OnOrderSuccess
                                ));
                          });
                    },
                  ),
fnatzsnv

fnatzsnv1#

您正在此处更改集合:

// remove to set to look like {}
emit(state.copyWith(
   subscriptions: state.subscriptions..remove(event.key)));

因此,该集仍然是同一示例,并且块不会重新渲染。
尝试使用Set。删除值的位置:

emit(state.copyWith(
   subscriptions: state.subscriptions.where((item) => item != event.key)).toSet();

相关问题