You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If copyWith of the base class that is overridden by the subclass is called, copyWith of the subclass should be called.
However, since extension will not be overridden in dart, copyWith of the base class will be called.
@CopyWith()
classBase {
int x;
Base({requiredthis.x});
BasecopyWith2({int? x}) =>Base(x: x ??this.x);
}
@CopyWith()
classExtendedextendsBase {
int y;
Extended({requiredsuper.x, requiredthis.y});
@overrideExtendedcopyWith2({int? x, int? y}) =>Extended(x: x ??this.x, y: y ??this.y);
}
voidmain() {
finalBasebase=Extended(x:1, y:2);
print(base.copyWith()); // flutter: Instance of 'Base' <-- should be Extendedprint(base.copyWith2()); // flutter: Instance of 'Extended'
}
Also, in such a design, Base may be abstract, in which case copy_with_extension generates code that cannot be compiled.
I have researched about this and found this solution:
Delete _$BaseCWProxyImpl and $BaseCopyWith. (which are implementations for an abstract class).
Change _$ExtendedCWProxy extends _$BaseCWProxy.
Change copyWith in $ExtendedCopyWith private. (Optional)
If copyWith of the base class that is overridden by the subclass is called, copyWith of the subclass should be called.
However, since extension will not be overridden in dart, copyWith of the base class will be called.
Also, in such a design, Base may be abstract, in which case copy_with_extension generates code that cannot be compiled.
I have researched about this and found this solution:
_$BaseCWProxyImpl
and$BaseCopyWith
. (which are implementations for an abstract class)._$ExtendedCWProxy
extends_$BaseCWProxy
.$ExtendedCopyWith
private. (Optional)Then, this can be achived as below:
Could you add options for this? Or is there any better way to do this?
The text was updated successfully, but these errors were encountered: