Manage state for nested classes using Flutter Provider

259

I am not too sure about the full scope of your project nor how far you are into it but I would highly suggest checking out the BLOC library as an alternative state management solution to simply manage the state in your case.

The learning curve is fairly low with many tutorials on how exactly to use the library alongside the fact that BLOC is much less reliant on listeners from an external persons view using the library from my experiences if that is what you are struggling with.

Here is a link to the BLOC Library

If you are still persistent to use Provider, I would suggest you create a provider for each model in a separate file for multiple reasons such as separation of concerns and ease of management of state and clarity of each different model.

Share:
259
jbly1309
Author by

jbly1309

Updated on December 25, 2022

Comments

  • jbly1309
    jbly1309 over 1 year

    Currently am working on Sport Event Scoring app. The structure of the app goes something like this:

    enter image description here

    Right now I am trying to include state management using provider package.

    Scoring occurs at the very bottom of the Widget tree using a counter and each Widget initializes respectively named model based on a user input on the go - creating Event, adding divisions and players each happens in a dialog.

    I know I need to create a provider for list of Events, however, I am not sure how to go about structuring rest of the state management. So far I tried turning each model into a Provider by mixin in the ChangeNotifier and moving all the methods which change the data inside where the state would be managed as well with notifyListeners(). However after a lot of struggling and searching I found that it's not the best approach to manage the state inside models.

    What approach would be ideal? Should I manage the state through the Event List provider, or should I go with creating a provider for each model in a separate file? If so, how?

    • totalitarian
      totalitarian about 3 years
      Hi, did you ever come up with a solution? I'm in the same situation.
  • jbly1309
    jbly1309 over 3 years
    Thank you very much for answering my question! I would like to ask you, if you could elaborate on why should I use bloc as opposed to provider which is a preferred way to go about state management? (according to what i found so far at least)
  • MattTheXPat
    MattTheXPat over 3 years
    Well its really personal preferences and what is good for the situation. For me, I found BLOC had a very low learning curve, is robust and easier to test for me. It's robust because you are easily able to predict the outcome for each event based of the input that is put into the bloc. But, in saying this, it doesn't mean you are unable to use multiple state management solutions in your app, for example, I use a few state management solutions in one app including Provider and BLOC. So don't feel that you are limited to only BLOC or provider. You can use both, together, and even others too!
  • MattTheXPat
    MattTheXPat over 3 years
    Also please don't forget to mark the answer as correct for future users to know which answer solved you question! :)