mutable_copy 0.2.2 mutable_copy: ^0.2.2 copied to clipboard
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 = Employee(
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 Employee {
final String fullName;
final String department;
final String team;
Employee({
this.fullName,
this.department,
this.team,
});
@override
String toString() {
return '$fullName, Department: $department, Team: $team';
}
}
Mutable copy extension #
extension EmployeeMutableCopyExt on Employee {
EmployeeMutable mutableCopy() {
return EmployeeMutable(
fullName: fullName,
department: department,
team: team,
);
}
Employee copy(UpdateWith<EmployeeMutable> updateWith) {
return updateWith(mutableCopy()).copy();
}
}
Mutable object #
class EmployeeMutable with Mutable<Employee> {
String fullName;
String department;
String team;
EmployeeMutable({
this.fullName,
this.department,
this.team,
});
@override
Employee copy() {
return Employee(
fullName: fullName,
department: department,
team: team,
);
}
}
Code generation #
Code generator for mutable_copy
import 'package:mutable_copy/mutable_copy.dart';
part 'employee.g.dart';
@imutable
@MutableCopy
class Employee {
final String fullName;
final String department;
final String team;
Employee({
this.fullName,
this.department,
this.team,
});
}
Add dependencies #
dependencies:
...
mutable_copy: ^0.2.2
dev_dependencies:
...
mutable_copy_generator: ^0.2.2
build_runner: ^1.9.0
Generate code #
flutter pub run build_runner build