Dart: Custom "copyWith" method with nullable properties

Issue

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.

Solution

Another solution is to use a function to set the value. This way, you can determine between a function that isn’t provided (null), a function that is provided and returns () => null, and a function that returns the name () => '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.

Answered By – Daniel Arndt

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.