Closed meynety closed 1 month ago
Hi, yes you found the right comment: https://github.com/schultek/dart_mappable/issues/215#issuecomment-2260235384
This is the same case. I would like to improve this, but its a limitation created by the inheritance structure that I cannot fix. All subclasses must have a parameter in their constructor for it to be usable in the superclasses copyWith.
I think the best workaround in your case is to make it a optional parameter with a default value:
class InitialState extends State with InitialStateMappable {
InitialState({super.aMember = false});
}
Use case
Hi there ! I’m trying to use the copyWith method in a Dart class hierarchy that involves inheritance. I’m not sure if I'm encountering a real issue or if I'm misunderstanding something. Apologies if it's the latter!
(Edit : Issue is similar to https://github.com/schultek/dart_mappable/issues/215#issuecomment-2260235384 but I believe that the use case is different enough to need a different solution.)
Here's the setup I'm working with:
Expected behavior
I expect the
copyWith
method to include all parameters from the base class constructor. In this example, it should includeaMember
.Actual Issue
The generated
copyWith
method does not includeaMember
as a parameter.Here is a screenshot of the issue :
Here is a test project that recreates this issue : dart-mappable-copy-with.zip
Findings and Workarounds
aMember
to theInitialState
constructor, thecopyWith
method works as expected. However, this defeats the purpose of havingInitialState
with a default value foraMember
.aMember
parameter in their constructors forcopyWith
to function correctly. If I add another subclass withoutaMember
, the copyWith method fails again :Questions: