ValueWrapper<T> class
A wrapper that disambiguates null from the absence of a value in copyWith methods.
Passing ValueWrapper(null) explicitly signals "set this field to null",
whereas omitting the parameter means "leave this field unchanged".
class MyObject {
final String? name;
MyObject({this.name});
MyObject copyWith({ValueWrapper<String?>? name}) {
return MyObject(
name: name != null ? name.value : this.name,
);
}
}
final original = MyObject(name: 'Initial Name');
// Do not update the name.
final notUpdated = original.copyWith();
print(notUpdated.name); // 'Initial Name'
// Explicitly set the name to null.
final updatedToNull = original.copyWith(name: ValueWrapper(null));
print(updatedToNull.name); // null
Constructors
- ValueWrapper(T value)
-
Wraps
valueto distinguish an explicitnullfrom an omitted parameter.const
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- value → T
-
The value this wrapper holds.
final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited