I wrote an app last year where I needed to update a model across multiple screens, so I used provider with changeNotifier. I implemented the provider like so;
class SurveyProvider with ChangeNotifier, DiagnosticableTreeMixin{
String name = "";
String postCode= "";
void addSurvey(Survey survey){
this.name = survey.name;
this.postCode = survey.postCode;
notifyListeners();
}
The provider is for the data model below. To keep things simple, I've reduced it to just two variables.
class Survey{
final String name;
final String postCode;
}
I implemented each variable of the model as a variable inside the provider. Should I have implemented the entire model inside the provider like this?
class SurveyProvider with ChangeNotifier, DiagnosticableTreeMixin{
Survey survey;
void addSurvey(Survey survey){
this.survey = survey;
notifyListeners();
}
I'm not sure which way is the correct way to do this, but I have a feeling the way I've done it is wrong.
There is not a universally correct approach to state management, but I would say that what you did was not wrong.
The first approach is better when you have a 1 to 1 link between the object and the provider.
However, the second option is more solid if you need a provider for complex and different entities, since it allows you to separate the objects logic from their provider's.
The best solution depends on the size and complexity of your project.