I am implementing a redux store where I treat the store as immutable. Therefore the store looks something like this:
class State {
final String firstname;
final String lastname;
State(this.firstname, this.lastname);
State.initial()
: this.firstname = null,
this.lastname = null;
State copy({String firstname, String lastname}) =>
new State(firstname ?? this.firstname, lastname ?? this.lastname);
}
This all works fine and I can copy the State and replace individual fields within the copy.
state.copy(firstname: "foo");
but now I can't set a field back to null of course.
state.copy(firstname: null);
doesn't work.
I also tried something like:
State copy({String firstname = this.firstname, String lastname = this.lastname}) => ...
but Dart does not allow non const as default values.
You can use a special marker in the file like this
const _noValueGiven = const Object();
class State {
...
State copy({String firstName: _noValueGiven, String lastName: _noValueGiven}) {
if (identical(firstName, _noValueGiven)) {
firstName = this.firstName;
}
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With