Flutter Provider Architecture for State Management | Flutter Provider



Flutter Architecture Guide using Provider for state management.

Written Tutorial:

GitRepo:

All Written Tutorials:

23 Comments

  1. If you are getting a dispose exception register the HomeModel as a Factory in the locator and not a singleton.

  2. Is this architecture still good with provider 4+ nd latest get_it? Soon Starting my first app.. Nd this is the only that makes sense to me.

  3. How to use the automaticKeppAliveClientMixin in provider architecture? Or is there any way to preserve state in navigators?

  4. Thank you, this video was very informative. I needed several hours to process all the info within…
    But I'm very confused as to why you decided to not inject the AuthenticationService for obtaining the User id in order to get the posts. you mention that "it does not make sense", but I don't really understand the reasoning. I have the following questions:
    1) How do you decide when to use a Stream or when to inject a service? do you follow any particular rules?
    2) What happens if you want/need to listen to multiple streams in your app? Do you surround your StreamProvider with another StreamProvider and so on for as many streams as you need? Does that even work?

  5. Hi, I need your help, I want to show "statusCode" and "reasonPhrase" on the top of the PostList UI(home_view) but I don't know how I can pass that from api >> getPostsForUser(int userId) method with the List<Post>.

    Future<List<Post>> getPostsForUser(int userId) async {

    var posts = List<Post>();

    // Get user posts for id

    var response = await client.get('$endpoint/posts?userId=$userId');

    int statusCode = response.statusCode;

    String message = response.reasonPhrase;

    var parsed = json.decode(response.body) as List<dynamic>;

    // loop and convert each item to Post

    for (var post in parsed) {

    posts.add(Post.fromJson(post));

    }

    return posts;

    }

  6. Bro i really love your tutorials. But I have one request from you is that Try to explain in more simple and in details πŸ™‚

  7. This line is not working on modern Android studio setup in start directory:
    (If I comment this line, all seems ok)

    Compiler message:

    lib/locator.dart:3:23: Error: Getter not found: 'instance'.

    GetIt locator = GetIt.instance;

    ^^^^^^^^

  8. Getting the following exception : ══║ EXCEPTION CAUGHT BY WIDGETS LIBRARY β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
    I/flutter (12748): The following assertion was thrown building HomeView:

    I/flutter (12748): type '(HomeModel) => Future<dynamic>' is not a subtype of type '(BaseModel) => dynamic'

  9. Thanks for the tutorial. I have a quick question. I'm using Firebase on the backend and I'm wondering what is the difference between the way you set up the StreamProvider in main with a controller and doing something like:
    StreamProvider<User>.value(value: (AuthenticationService().getUser))
    where getUser is a function in AuthenticationService that is:
    return _authentication.onAuthStateChanged(user)), where _authentication is a FirebaseAuth instance. Let me know if I can clarify.

  10. Hey great video, you have many repositories in your git, which one correlates to this video so I can download and follow it?

  11. Thank you so much for creating these amazing tutorials.

    I'm new to flutter, I'm trying to implement your "Navigating without context" tutorial into this "Provider Architecture" Login and Posts example.

    I'm having issue when I'm trying to replace

    "Navigator.pushNamed(context, '/post', argument: posts[index]);"

    because this is inside a method "getPostsUi" and I don't have access to the "model".

    I have crated a function in home_model:

    void goToScreen(dynamic argument){

    _navigationService.navigatTo(routes.PostRoute, arguments: argument);

    }

    Please let me know if function is right and how to get access to "model" in Widget methods if I don't want to pass it as a argument to widget method.

  12. Hello Dane, first of all i've been watching your tutorials lately and i find them the best, thanks man!
    I have a question, i used the same way you mentioned in this video to build my flutter APP that deals with an API i made, i face a problem that the first time i open the APP the user id entered is not taken in consideration, instead initial value is used (0), then going back to login and login again dose the magic. I suspect the StreamProvider dosen't supply User to homeview on the first time. I tried a lot, the values stuck on 0 as a first time. any thoughts please?

  13. Hi, any advice for using Provider with View Models that contain other View Models? Couple of examples:
    1. Form with n+ input fields, with validation, and a submit button that is only enabled when all input fields are valid
    2. List of items where each item is a view model (maybe they can be in expanded/unexpanded state).

    I've tried this and its been very cumbersome.

    In case 1 I had a separate vm that has contained input data, validation state and validation rules. My goal was not to make the entire form rebuild when I the submit button changed state. To meed the goal I had to make the main vm listen to changes on the field vm and in the listener I updated the submit button state. To keep the entire form from being updated I used a value listener on the submit button and did not derive main vm from ChangeNotifier.

    In the package issues section there was a question similar to the above and the package author did not think it was a typical use case( my interpretation) and author suggested using multi/proxy providers to create the field vm separate and add get/set to main vm to add field vm to it. Cant say I'm to fond of that suggestion.

    Thanx for any help

    JA

  14. You are superb. Video Quality is amazing, and written tutorial is awesome. Github is wonderful! Can't live without it.

  15. Not a programmer but tried to learn Flutter. On building my first app, should I immediately apply this architecture (provider) ? Is this concept for the beginners?

  16. Hey. Thanks for this tutorial. I have one question in mind. How to structure the provider if i have multiple consumers scattered down the widget tree? cant use the base view since it places the consumer right below it. Should i just go the normal way of placing the provider on the nearest common widget? thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *