1

I need to compare a list of map (i.e List < Map >) in dart using equatable for to be used inside a bloc state class. But the problem is that equatable seems to not compare that list of map properties.

class WaterCartDetailState extends Equatable {
  final String? status;
  final WateCartDetailModel? result;
  final List<Map>? mapData;
  const WaterCartDetailState({this.status, this.result, this.mapData});

  WaterCartDetailState copyWith(
      {String? status, WateCartDetailModel? result, List<Map>? mapData}) {
    return WaterCartDetailState(
        status: status ?? this.status,
        result: result ?? this.result,
        mapData: mapData ?? this.mapData);
  }

  @override
  List<Object?> get props => [status, result, mapData];
}

Since equatable is not comparing the List of Map. I cannot emit new state changes.

BestOFBest
  • 43
  • 7

1 Answers1

3

From Docs

Equatable properties should always be copied rather than modified. If an Equatable class contains a List or Map as properties, be sure to use List.from or Map.from respectively to ensure that equality is evaluated based on the values of the properties rather than the reference.

yield WaterCartDetailState(status, result, List<Map>.from(mapData));
Eslam Ashraf
  • 316
  • 3
  • 7