Dart enhanced enums as map keys - Cannot modify unmodifiable map

601 Views Asked by At

I am migrating from this enum extension:

enum VgnItmTypeEnum {
  @JsonValue(0)
  nullObject,
  @JsonValue(1)
  groceryItm
}

extension VgnItmType on VgnItmTypeEnum {
  static Map<VgnItmTypeEnum, Option> items = <VgnItmTypeEnum, Option>{
    VgnItmTypeEnum.groceryItm: Option(
        name: 'Grocery Item',
        id: VgnItmTypeEnum.groceryItm.index,
        iconCodePoint: 0xf291),
    VgnItmTypeEnum.nullObject: Option(
        name: '', id: VgnItmTypeEnum.nullObject.index, iconCodePoint: 0xf07a)
  };

  String get name => items[this]!.name;
  int get id => items[this]!.id;
  int get iconCodePoint => items[this]!.iconCodePoint;
  Option get option => items[this]!;
}

to this enhanced enum:

enum VgnItmType {
  nullObject(value: Option(name: 'Null Object', iconCodePoint: 0xf07a, id: 0)),
  groceryItm(value: Option(name: 'Grocery Item', iconCodePoint: 0xf291, id: 1)),
  );

  const VgnItmType({required this.value});
  final Option value;

  static Map<VgnItmType, Option> items = <VgnItmType, Option>{
    VgnItmType.groceryItm: Option(
        name: 'Grocery Item',
        id: VgnItmType.groceryItm.index,
        iconCodePoint: 0xf291)
    VgnItmType.nullObject:
        Option(name: '', id: VgnItmType.nullObject.index, iconCodePoint: 0xf07a)
  };

  String get name => items[this]!.name;
  int get id => items[this]!.id;
  int get iconCodePoint => items[this]!.iconCodePoint;
  Option get option => items[this]!;

}

I was using VgnItmTypeEnum as Map keys. Now that I have upgraded to use the VgnItmType enhanced enum as my map keys, I get an error on this code which updates a Map value for a given map key (vgnItms):

@freezed
class VgnItmCache extends Entity
    with LocalSaveMixin<VgnItmCache>, _$VgnItmCache {
  const factory VgnItmCache(
      {Map<VgnItmType, VgnItmEst>? vgnItms,
      @Default(<S3ImageCommand>[]) List<S3ImageCommand> s3ImageCommands,
      Option? vgnItmType,
      FormType? formType,
      @JsonKey(ignore: true) Ref? provider}) = _VgnItmCache;

// Here
      void setVgnItm({required VgnItm vgnItm, VgnItmType? type}) {
        final theType = type ?? myVgnItmType;
        vgnItms![theType] = vgnItms![theType]!.copyWith(vgnItm: vgnItm);
      }

error:

Unsupported operation, Cannot modify unmodifiable map.

EDIT here is how I construct the VgnItmCache.vgnItms (note that VgnItmType.items is on the enum at the top of the question):

  @override
  VgnItmCache $localFetch() {
    var cache = VgnItmCache.fromJson(Map<String, dynamic>.from(
        localDataSource.$localFetch<String>(keyToRead: ADD_VEGAN_ITEM)));

    final populatedVgnItms = VgnItmType.items.map((k, v) { 
      return MapEntry(k, cache.vgnItms?[k] ?? VgnItmEst.empty(k));
    });

    cache = cache.copyWith(vgnItms: populatedVgnItms);

    return cache;
  }
1

There are 1 best solutions below

0
On

It is actually caused by upgrading Freezed from 1 to 2. Using @Freezed(makeCollectionsUnmodifiable: false) fixed the issue. I had upgraded a few things incase they were necessary for enhanced enums (json_seriablizable did need to be upgraded to at least 6.1.5).