目前在我的应用程序中,我管理一个键值映射,在该变量中,我有一个列表,删除和追加,随着时间的推移,它增加,我发出它到我的Bloc状态能够改变UI,但它不改变它,但追加它,并删除它,但不改变它,有人知道为什么吗?
void _onShape(
AddShapeEvent event,
Emitter<DrawingAnnotationState> emit,
) {
final Shape shape = event.shape;
final newMap = Map.of(state.shapesByPages); // getting the map in state
/// The shape is added to the page
newMap[currentPage]!.removeWhere((l) => l.id == shape.id);
newMap[currentPage]!.add(shape);
emit(state.copyWith(
status: Status.success,
lineList: newMap[currentPage],
shapeMap: newMap,
));
}
* * DrawingAnnotationState * *
enum DrawingAnnotationStatus { initial, loading, success, failure }
extension DrawingAnnotationStatusX on DrawingAnnotationStatus {
bool get isInitial => this == DrawingAnnotationStatus.initial;
bool get isSuccess => this == DrawingAnnotationStatus.success;
bool get isFailure => this == DrawingAnnotationStatus.failure;
bool get isLoading => this == DrawingAnnotationStatus.loading;
}
class DrawingAnnotationState extends Equatable {
final DrawingAnnotationStatus status;
final List<DrawnShape> lineList;
final Map<int, List<DrawnShape>> shapesByPages;
final String failureMessage;
const DrawingAnnotationState({
this.status = DrawingAnnotationStatus.initial,
this.lineList = const [],
this.shapesByPages = const {},
this.failureMessage = "",
});
DrawingAnnotationState copyWith({
DrawingAnnotationStatus? status,
List<DrawnShape>? lineList,
Map<int, List<DrawnShape>>? shapesByPages,
String? failureMessage,
}) {
return DrawingAnnotationState(
status: status ?? this.status,
lineList: lineList ?? this.lineList,
shapesByPages: shapesByPages ?? this.shapesByPages,
failureMessage: failureMessage ?? this.failureMessage,
);
}
@override
List<Object?> get props {
return [
status,
lineList,
shapesByPages,
failureMessage,
];
}
}
——编辑
我在这里看到,我的List是一个类的列表,它有另一个列表,这个列表是一个正在增加的列表,我这样做只是为了不重复它们。
我昨天面临同样的问题,我假设你已经删除然后添加相同的项目到shapesByPages
,我猜Bloc比较地图与相同的键和值不会触发生成器,甚至创建新的Map实例
我的解决方法是先发出空映射:
emit(shapeMap: {})
然后
emit(state.copyWith(
status: Status.success,
lineList: newMap[currentPage],
shapeMap: newMap,
));