Flutter 对象设置器中的重复数据

Flutter 对象设置器中的重复数据,flutter,dart,Flutter,Dart,我有一个用于作用域模型的用户对象: 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 updateUse

我有一个用于作用域模型的用户对象:

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中的用户构造函数复制代码?

如果调用update方法中的第一个构造函数,看起来会更简单。
在它们内部调用“用户(数据)”,您可以使用“notifyListeners();”

这是一种方法:

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();
        }
    }
}