我有一个用于范围模型的用户对象:
class User extends Model {
String name;
String description;
// and a bunch of other fields
User(Map data) {
this.name = data['name'];
this.description = data['description'];
}
void updateUser(Map data) {
// copy and pasted from User constructor
this.name = data['name'];
this.description = data['description'];
notifyListeners();
}
}
是否有一种方法不必从updateUser内部的User构造函数中复制代码?
答案 0 :(得分:0)
如果在update方法中调用第一个构造函数,它将看起来更简单。 在其中调用“ User(data)”,然后可以输入“ notifyListeners();”
答案 1 :(得分:0)
这将是一种实现方式:
class User extends Model {
String name;
String description;
// and a bunch of other fields
User(Map data) {
updateUser(data, notify: false);
}
void updateUser(Map data, {bool notify = true}) {
// copy and pasted from User constructor
this.name = data['name'];
this.description = data['description'];
if(notify) {
notifyListeners();
}
}
}