mutable_copy 0.2.5

Dart native js
Flutter Android iOS web

MutableCopy simplify copy of immutable object. Unlike CopyWith, it allow to set null value.

mutable_copy #

MutableCopy simplify copy of immutable object. Unlike CopyWith allow to set null value

Usage #

Copy object with help mutable copy. #

final e1 = Employe(
  fullName: 'Max Hatson',
  department: 'Mobile',
  team: 'Flutter',
);
final eMutable = e1.copy((o) => o..fullName = 'Bob Watson');

Import #

import 'package:mutable_copy/mutable_copy.dart';

Sample object #

class Employe {
  final String fullName;
  final String department;
  final String team;

  Employe({
    this.fullName,
    this.department,
    this.team,
  });

  @override
  String toString() {
    return '$fullName, Department: $department, Team: $team';
  }
}

Mutable copy extension #

extension EmployeMutableCopyExt on Employe {
  EmployeMutable mutableCopy() {
    return EmployeMutable(
      fullName: fullName,
      department: department,
      team: team,
    );
  }

  Employe copy(UpdateWith<EmployeMutable> updateWith) {
    return updateWith(mutableCopy()).copy();
  }
}

Mutable object #

class EmployeMutable with Mutable<Employe> {
  String fullName;
  String department;
  String team;

  EmployeMutable({
    this.fullName,
    this.department,
    this.team,
  });

  @override
  Employe copy() {
    return Employe(
      fullName: fullName,
      department: department,
      team: team,
    );
  }
}

Code generation #

Code generator for mutable_copy

import 'package:mutable_copy/mutable_copy.dart';

part 'employe.g.dart';

@imutable
@MutableCopy
class Employe {
  final String fullName;
  final String department;
  final String team;

  Employe({
    this.fullName,
    this.department,
    this.team,
  });
}

Add dependencies #

dependencies:
  ...
  mutable_copy: ^0.2.5

dev_dependencies:
  ...
  mutable_copy_generator: ^0.2.5
  build_runner: ^1.9.0

Generate code #

flutter pub run build_runner build
2
likes
110
pub points
51%
popularity

MutableCopy simplify copy of immutable object. Unlike CopyWith, it allow to set null value.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

alexvegner@gmail.com

License

MIT (LICENSE)

More

Packages that depend on mutable_copy