freezed_collection 2.1.0-pre1 copy "freezed_collection: ^2.1.0-pre1" to clipboard
freezed_collection: ^2.1.0-pre1 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.0.0-pre1

Status #

FreezedMap can be used.

FreezedList and FreezedSet are coming soon.

copyWith #

@freezed
abstract class One with _$One {
  const factory One(String name, Two two) = _One;

  factory One.fromJson(Map<String, dynamic> json) => _$OneFromJson(json);
}

@freezed
abstract class Two with _$Two {
  const factory Two(int index, FreezedMap<String, int> three) = _Two;

  factory Two.fromJson(Map<String, dynamic> json) => _$TwoFromJson(json);
}

final one = One('a', Two(1, FreezedMap({'1': 2, '3': 4})));
final two = one.copyWith.two.three.addAll({'5': 6, '7': 8}).seal();

final json = jsonEncode(two.toJson());
final dec = One.fromJson(jsonDecode(json));
expect(dec.two.three.toMap(), equals({'1': 2, '3': 4, '5': 6, '7': 8}));
final dec2 = One.fromJson(jsonDecode(
  '{"name":"a","two":{"index":1,"three":{"1":2,"3":4,"5":6,"7":8}}}'));
expect(dec2.two.three.toMap(), equals({'1': 2, '3': 4, '5': 6, '7': 8}));

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();
  • FreezedList<T>.fromJson
  • FreezedList<T>.copyWith.replaceFirst(T newElement, bool Function(T element) what)

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.

4
likes
0
points
1.68k
downloads

Publisher

verified publishermtbo.org

Weekly Downloads

Extending dart:freezed to deep copyWith for collection.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

freezed_annotation, json_annotation

More

Packages that depend on freezed_collection