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
Hello
I noticed a situation that I think is a problem, or that I could not find a solution to it
When one class inherits from another class
I can't influence variables to be of type: immutable
example here: ↓
if any solution for this
and thanks.
part 'square_model.g.dart';
@CopyWith()
class Square extends Shape {
final String name;
Square({
required super.id,
required this.name,
});
}
abstract class Shape {
@CopyWithField(immutable: true)
final String id;
Shape({required this.id});
}
Future<void> main() async {
Square square = Square(id: '123', name: 'test');
square.copyWith(
id: '333', // ==> this has to be immutable
name: 'test2',
);
}
The text was updated successfully, but these errors were encountered:
I understand your concern. If the class’s field is inherited and initially immutable, you want to maintain its immutability in the subclass.
However, I’m not entirely sure if this is the best approach. After all, it’s a new class. While it might be the right choice, it could also cause problems for existing users. It could introduce the opposite scenario where you want to make the field mutable instead.
Unfortunately, I don’t have a good solution for this problem at the moment. Overriding the field in the subclass would yield warnings, and approaching the problem from the opposite direction, following immutability, would also lead to issues with being unable to change the definition.
In my opinion, the current implementation is the least of two evils as I don’t have a better solution at the moment.
abstractclassShape {
Shape({requiredthis.id});
@CopyWithField(immutable:true)
finalString id;
}
@CopyWith()
classSquareextendsShape {
Square({
// Will appear in copyWith as builder doesn't see the definition of `CopyWithField` in this subclass.requiredsuper.id,
requiredthis.name,
});
finalString name;
}
Hello
I noticed a situation that I think is a problem, or that I could not find a solution to it
When one class inherits from another class
I can't influence variables to be of type: immutable
example here: ↓
if any solution for this
and thanks.
The text was updated successfully, but these errors were encountered: