I'm working on a Flutter project in which the architecture forces me to use an instance of a class, say UserModel
, as a value attached to the BuildContext
. In this way, I can access that class in every part of the app.
Anyway, there are moments in which I want to update the UserModel
, and not only by changing the value of some of the fields, but updating all of them. At this moment, I am using an horrible function, let's call that updateUser(final UserModel newInstance)
. It follows an example.
class UserModel {
String name, surname;
UserModel({required this.name, required this.surname});
void updateUser(final UserModel newInstance) {
name = newInstance.name;
surname = newInstance.surname;
}
}
The main problem with this solution is that it often happens that I add some fields to the UserModel
class, but I forgot to add them to updateUser
. Also, the solution I am using looks horrible.
I know that many of you could say that I should enclose the instance of UserModel
in a UserModelProvider
class and just replace the value of the userModel
field. This would not be compatible with the architecture of the software I am developing, so please don't suggest me that.