We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Generate copyWith method for sealed classes.
We should disallow generic type change in copy with method to make life easier.
For nullable types we have two approaches:
For example:
class Base{ void one(int? x); }
Freezes way of implementation:
this will generate code which needs two levels of inheritance for each copy with method and is not very straight forward.
we have this:
/// @nodoc abstract class $BaseCopyWith<$Res> { factory $BaseCopyWith(Base value, $Res Function(Base) then) = _$BaseCopyWithImpl<$Res>; } /// @nodoc class _$BaseCopyWithImpl<$Res> implements $BaseCopyWith<$Res> { _$BaseCopyWithImpl(this._value, this._then); final Base _value; // ignore: unused_field final $Res Function(Base) _then; }
then this:
/// @nodoc abstract class _$OneCopyWith<$Res> { factory _$OneCopyWith(_One value, $Res Function(_One) then) = __$OneCopyWithImpl<$Res>; $Res call({int? x}); } /// @nodoc class __$OneCopyWithImpl<$Res> extends _$BaseCopyWithImpl<$Res> implements _$OneCopyWith<$Res> { __$OneCopyWithImpl(_One _value, $Res Function(_One) _then) : super(_value, (v) => _then(v as _One)); @override _One get _value => super._value as _One; @override $Res call({ Object? x = freezed, }) { return _then(_One( x: x == freezed ? _value.x : x // ignore: cast_nullable_to_non_nullable as int?, )); } }
@JsonKey(ignore: true) _$OneCopyWith<_One> get copyWith => throw _privateConstructorUsedError;
which will be overriden by:
@JsonKey(ignore: true) @override _$OneCopyWith<_One> get copyWith => __$OneCopyWithImpl<_One>(this, _$identity);
And using optionals:
class Optional<T> { final bool isValid; final T? _value; T get value => _value as T; const Optional() : isValid = false, _value = null; const Optional.value(this._value) : isValid = true; } class Person { final String? name; Person(this.name); Person copyWith({Optional<String?> name = const Optional()}) => Person(name.isValid ? name.value : this.name); }
But when you want to set to any thing you should use optional ...
Special thanks to @iamarnas for his recommendations.
The text was updated successfully, but these errors were encountered:
@iamarnas
Sorry, something went wrong.
No branches or pull requests
Generate copyWith method for sealed classes.
We should disallow generic type change in copy with method to make life easier.
For nullable types we have two approaches:
For example:
Freezes way of implementation:
this will generate code which needs two levels of inheritance for each copy with method and is not very straight forward.
we have this:
then this:
we have this:
which will be overriden by:
And using optionals:
But when you want to set to any thing you should use optional ...
Special thanks to @iamarnas for his recommendations.
The text was updated successfully, but these errors were encountered: