freezed_collection 2.2.0+1 copy "freezed_collection: ^2.2.0+1" to clipboard
freezed_collection: ^2.2.0+1 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.2.0

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}).build();

Element access:

  final list = FreezedList([1, 2, 3]);
  final list2 = (list.copyWith..[1] = 0).build(); // [1, 0, 3]

Building #

copyWith returns mutable builder with methods mapped to corresponding collection interface.

build() method should be used to build freezed collection (previously known as seal()).

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)
    .build();

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.

5
likes
160
points
291
downloads

Documentation

API reference

Publisher

verified publishermtbo.org

Weekly Downloads

Extending dart:freezed to deep copyWith for collection.

Repository (GitHub)
View/report issues

License

BSD-3-Clause (license)

Dependencies

freezed_annotation, json_annotation

More

Packages that depend on freezed_collection