-
Notifications
You must be signed in to change notification settings - Fork 1
copyWith
probably handles nulls incorrectly
#3
Comments
That is indeed true, because if a parameter to the method is Use mutable for copying Use wrapper function mySister = me.copyWith(
firstName: 'Yvonne',
photoUrl: () => null,
) Default values const _default = Object();
int hashValues(Object arg1, [Object arg2 = _default, ...]) In the code you can check if If anyone has any other suggestions on how to do this elegantly, you're welcome to share your thoughts. |
I guess there is also "use mutable in copyWith" option which is similar to builders in BuiltValue:
|
Oh true, that would basically be the first option with some syntactic sugar on top. |
Thought about this a bit more. |
What params will So I'm in favor of keeping things simple with a single interface for mutations. |
You're right. What about we always have a builder method for mutations but if your class has only non-nullable fields, you can optionally explicitly ask for the And if the class contains nullable fields, that will be an error during code generation. |
I'm still in favor of E.g. requirements change and a field becomes nullable. A developer will need to remove |
Hmm that is indeed true... |
Done. The newest version 2.0.0, already published on pub, generates a var freshApple = const Fruit(type: 'apple', color: 'green');
var someApple = freshApple.copy((fruit) => fruit..color = null);
var kiwi = someApple.copy((fruit) => fruit
..type = 'Kiwi'
..color = 'brown'); The definition looks like this: /// Copies this [Fruit] with some changed attributes.
Fruit copy(void Function(MutableFruit mutable) changeAttributes) {
assert(
changeAttributes != null,
"You called Fruit.copy, but didn't provide a function for changing "
"the attributes.\n"
"If you just want an unchanged copy: You don't need one, just use "
"the original.");
var mutable = this.toMutable();
changeAttributes(mutable);
return Fruit.fromMutable(mutable);
} |
hi,
I'm looking into your example and I guess nulls do not work properly(I didn't run it though).
photoUrl
is updated as followsphotoUrl: photoUrl ?? this.photoUrl,
which leads tomySister.photoUrl == 'http://example.com'
instead of null.The text was updated successfully, but these errors were encountered: