Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CopyWith to allow setting values to null - like spread operator #40

Closed
atreeon opened this issue Apr 25, 2018 · 6 comments
Closed

CopyWith to allow setting values to null - like spread operator #40

atreeon opened this issue Apr 25, 2018 · 6 comments

Comments

@atreeon
Copy link

atreeon commented Apr 25, 2018

In the following code, I use the CopyWith pattern. One problem I'm experiencing with this is if I want to set a property to a null value (this is quite easy using the spread operator in js). The value of the name field of the 'Joe' object below is not null, it is 'Bob'. Do you know of any way to overcome this problem in Dart?

void main() {
  var bob = new Person(age: 15, name: 'Bob');
  print(bob.toString());
  var joe = bob.copyWith(name: null);
  print(joe.toString());
}

class Person {
  final int age;
  final String name;

  Person({this.age, this.name});

  Person copyWith({int age, String name}) {
    return new Person(
      age: age ?? this.age,
      name: name ?? this.name,
    );
  }
}

@atreeon
Copy link
Author

atreeon commented Apr 25, 2018

This would work but maybe there is a more succinct and descriptive method.

void main() {
  var bob2 = new Person2(age: 15, name: 'Bob');
  print(bob2.toString());

  var joe2 = bob2.copyWith(name: new Nullable<String>(null));
  print(joe2.toString());
}

class Person2 {
  final int age;
  final String name;

  Person2({this.age, this.name});

  Person2 copyWith({Nullable<int> age, Nullable<String> name}) {
    return new Person2(
      age: age == null ? this.age : age.value,
      name: name == null ? this.name : name.value,
    );
  }

  @override
  String toString() {
    return 'age: $age, name: $name';
  }
}

class Nullable<T> {
  T _value;

  Nullable(this._value);

  T get value {
    return _value;
  }
}

@atreeon atreeon changed the title CopyWith allowing setting values to null - like spread operator CopyWith to allow setting values to null - like spread operator Apr 25, 2018
@brianegan
Copy link
Owner

Hey there -- yep, in this case, you'd need an extra class like the one you've proposed. You can either create a class just as you've done, or consider using the Optional class from the quiver library: https://pub.dartlang.org/packages/quiver

@atreeon
Copy link
Author

atreeon commented Apr 28, 2018

That works a little nicer than my Nullable. I do love the javascript spread operator for this kind of thing though...I wonder if there was a way the Dart team could implement something like this (I'm guessing no due to the typed quality of the lanaguage...it does make my code nice and concise...I still prefer dart to js though!)

@brianegan
Copy link
Owner

Yah, I really hope they even get "data classes" like Kotlin, which would make this type-safe and easy. Please let me know if I can help further!

@pumuckelo
Copy link

Hi @brianegan , I just stumbled on the same problem and randomly found the solution to why my state isn't updating by randomly reading the copyWith Function.

What do you think of if we add a warning or information to the example of flutter_redux where the copyWith Method is shown? It's easier for other people then

@lukas-pierce
Copy link

class Nullable {
T _value;

Nullable(this._value);

T get value {
return _value;
}
}

class Nullable<T> {
  final T? _value;

  const Nullable(this._value);

  T? get value => _value;
}

adam-sroka referenced this issue in DigitalNutritionalAssessment/gibsonify May 21, 2022
Null measurement unit and value after changing the measurement method. This was initially attempted to be implemented by sending two more events when modifying the method, namely the events to null the unit and value, which yielded a very tricky to find bug related to indexing of FoodItems by indexOf(), which was (partially) fixed to indexing by indexWhere() using UUIDs. This needs to be fully fixed in the future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants