iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😺

The List returned by Dart's Enum.values is unmodifiable

に公開

The List obtained from Dart's Enum values function is an UnmodifiableList, so destructive changes such as remove cannot be performed.
By creating a new List with toList(), you can perform destructive changes.

See the code below:

enum ItemType {
    item1,
    item2,
    item3,
}

final items = ItemType.values; /// -> Returns [item1, item2, item3]
items.remove(ItemType.item1); /// Throws an error message: "Unsupported operation: remove"

final newItems = items.toList();
newItems.remove(ItemType.item1); /// -> Becomes [item2, item3]

I got stuck on this today.

Discussion