I'm trying to create a "copyWith" method for my class, and it works with most scenarios.
The problem is when I try to set a nullable property to null, because my function cannot recognize whether it's intentional or not.
Ex.:
class Person {
final String? name;
Person(this.name);
Person copyWith({String? name}) => Person(name ?? this.name);
}
void main() {
final person = Person("Gustavo");
print(person.name); // prints Gustavo
// I want the name property to be nul
final person2 = person.copyWith(name: null);
print(person2.name); // Prints Gustavo
}
Does anyone knows some workaround for this situation? This is really bothering me and I don't know how to avoid this situation.
One solution is to use a function to set the value. This gives you more options.
null
() => null
null
() => 'Gustavo'
Gustavo
class Person {
final String? name;
Person(this.name);
Person copyWith({String? Function()? name}) =>
Person(name != null ? name() : this.name);
}
void main() {
final person = Person('Gustavo');
print(person.name); // prints Gustavo
// I want the name property to be nul
final person2 = person.copyWith(name: () => null);
print(person2.name); // Prints null
final person3 = person.copyWith(name: () => 'new name');
print(person3.name); // Prints new name
final person4 = person.copyWith();
print(person4.name); // Prints Gustavo
}
It makes setting the name slightly more cumbersome, but on the bright side the compiler will tell you that you've provided the wrong type if you try to pass a string directly, so you will be reminded to add the () =>
to it.