Dart: Custom “copyWith” method with nullable properties

One solution is to use a function to set the value. This gives you more options.

  • A function that isn’t provided: null
    • This will not change the value
  • A function that is provided and returns null: () => null
    • This will set the value to null
  • A function that returns the name: () => 'Gustavo'
    • This will set the value to 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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)