0

I have a user object I'm using with scoped model:

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

Is there a way not have to duplicate the code from the User constructor inside updateUser?

2 Answers 2

0

This would be one way to do it:

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

it'll look simpler if you call the first constructor inside the update method. call "User(data)" inside them you can follow with "notifyListeners();"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.