我在Stackoverflow上看了几个有关此问题的问题,但没有一个让我明白。什么是最简单的方法?
请参考以下类:
class Customer {
final String id;
final String name;
final String address;
final String phoneNo;
final String gstin;
final String state;
Customer({
this.id = '',
@required this.name,
@required this.address,
@required this.phoneNo,
this.gstin,
@required this.state,
});
Customer copyWith({
String name,
String address,
String phoneNo,
String gstin,
String state,
}) {
return Customer(
name: name ?? this.name,
address: address ?? this.address,
phoneNo: phoneNo ?? this.phoneNo,
gstin: gstin ?? this.gstin,
state: state ?? this.state,
);
}
}
copyWith
现在是最好的方式(你可以查看Flutter Theme的工作原理)。你可以自己使用copyWith
或者使用类似freezed的代码生成器包。 - yellowgray