0

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;
  }
BeniaminoBaggins
  • 11,202
  • 41
  • 152
  • 287
  • In general, it would help both yourself and readers if you made an effort to distill your problems down into small, reproducible examples. Doing so probably would quickly refute your premise that this is related to enhanced `enum`s. The error is clear: you're trying to modify an unmodifiable `Map`, so how is `vgnItms` constructed? – jamesdlin Jul 09 '22 at 05:04
  • @jamesdlin Yes agreed, I will work on that now. I have added the constructing code to the bottom of the question. – BeniaminoBaggins Jul 09 '22 at 05:25

1 Answers1

0

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).

BeniaminoBaggins
  • 11,202
  • 41
  • 152
  • 287