freezed_collection 0.0.3 copy "freezed_collection: ^0.0.3" to clipboard
freezed_collection: ^0.0.3 copied to clipboard

Extending dart:freezed to deep copyWith for collection.

example/freezed_collection_example.dart

import 'package:freezed_collection/freezed_collection.dart';

import 'dart:convert';

import 'assistant.dart';
import 'company.dart';
import 'director.dart';

void printT<T>(T company) => print(json.encode(company));

void main() {
  final company = Company(
      name: '1',
      director: Director(
          name: 'director1',
          assistants: FreezedList(
              [Assistant(age: 1, name: '1'), Assistant(age: 3, name: '3')])));

  final jsonString = json.encode(company);
  final restoredCompany = Company.fromJson(json.decode(jsonString));

  print(restoredCompany);

  Company? newCompany = company.copyWith.director.assistants.replaceFirstWhere(
    Assistant(age: 2, name: '2'),
    (element) => element.age == 1,
  );

  Company? newCompany2 =
      newCompany.copyWith.director.assistants.replaceFirstWhere(
    Assistant(age: 1, name: '1'),
    (element) => element.age == 2,
  );

  printT(company);
  printT(newCompany);
  printT(newCompany2);

  print('Is newCompany equal to company? ${newCompany == company}');
  print('Is newCompany equal to company? ${newCompany2 == company}');
}
3
likes
110
pub points
73%
popularity

Publisher

verified publishermtbo.org

Extending dart:freezed to deep copyWith for collection.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

freezed_annotation, json_annotation

More

Packages that depend on freezed_collection