freezed_collection 2.1.5
freezed_collection: ^2.1.5 copied to clipboard
Extending dart:freezed to deep copyWith for collection.
Freezed Collection #
Extending dart Freezed
collection with deep copyWith
.
Install #
dependencies:
freezed_collection: ^2.1.5
Status #
- ✅
FreezedList<T>
- ✅
FreezedMap<T>
- ✅
FreezedSet<T>
copyWith #
Given:
@freezed
abstract class One with _$One {
const factory One(String name, Two two) = _One;
}
@freezed
abstract class Two with _$Two {
const factory Two(int index, FreezedMap<String, int> three) = _Two;
}
final one = One('a', Two(1, FreezedMap({'1': 2, '3': 4})));
then you can use copyWith
chain:
final two = one.copyWith.two.three.withBase(() =>
SplayTreeMap()).remove('1').addAll({'5': 6, '7': 8}).seal();
Sealing #
copyWith
returns mutable builder with methods mapped to corresponding collection interface.
seal()
method should be used to build freezed collection.
Chaining #
final map = FreezedMap({'1': 1});
final map2 = map.copyWith(map: {'2': 2, '3': 3})
.updateAllValues((k, p0) => p0 + 1)
.removeWhere((k, v) => 2 == v)
.seal();
Json #
If FreezedMap<K,V>
has non-standard (non-string) key, it will be encoded using json.encode
and decoded using
json.decode
. This leads to some performance leakage. So use String
for keys if you plan to serialize.