I want to implement copyWith()
method for my class. Since its inhirited class, i got error:
The member being overridden.
my basemodel
class BaseData {
int id;
String? createdAt, updatedAt;
BaseData({this.id = 0, this.createdAt, this.updatedAt});
BaseData copyWith({
int? id,
String? createdAt,
String? updatedAt,
}) =>
BaseData(
id: id ?? this.id,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt);
}
and I have another class that extend to my BaseData
model
class QcData extends BaseData{
String name;
QcData ({this.name =""});
@override
QcData copyWith({
String? name
}){
return QcData(name: name ?? this.name);
}
}
How to create copyWith()
method for my model class?
Thanks in advance.
2
Answers
inheritance means that a class member’s definitions should remain the same when you extend, or implement a base class, in your case by doing the:
this method declaration is not the same as the
BaseData
‘s since they take different sets of arguments, you should remain the arguments the same as theBaseData
, so your code should be this in order to work:We also need to pass data on while creating QcData(child). I prefer modifying constructor this way.
Now we have access on copyWith method these property, another cause is I like to use
const
constructor with final fields.The models will be