Closed zoechi closed 5 years ago
Are there any plans on backing an official state management solution opposed to having several competing libraries/standards?
There is no one-size-fits-all approach
Ok so bumping into this snag on two project
Whats missing in the docs et in ecosystem is the techniques to do this, for muppets...
I'm making slow (but steady) progress on this.
Today, I landed the simplest possible example to github.com/flutter/samples — the Counter app, using ScopedModel
. This should alleviate one of the most pressing issues: that there is no official sample that shows a Flutter app whose state is modified from "outside". Right now there's just the code (main.dart here). I'm working on an article that uses this example.
As @zoechi says, there is no one-size-fits-all, so please don't expect one single approach for state management. That said, we want to provide some starting points. ScopedModel
has a good balance of easy-to-understand, easy-to-use, and stable.
Adding a link to a blog https://medium.com/flutter-community/let-me-help-you-to-understand-and-choose-a-state-management-solution-for-your-app-9ffeac834ee3
One size fits all would be nice..
Docs have been slightly improved by https://github.com/flutter/website/pull/2332.
New pages start here: https://flutter.io/docs/development/data-and-backend/state-mgmt/intro
I would love your opinion on this. If you find that the current doc is still lacking (and it probably is), please don't hesitate to file a new bug with concrete suggestions. (And cc me.)
@filiph just curious what your thoughts are on https://github.com/felangel/bloc?
Hi @felangel, this looks really good!
My plan next is to invite package owners like you to contribute to the /options page. That way your work and the work of others is more visible. I do need to make sure the contributions don't get out of hand, though, so stay tuned for updates.
@Michael77 commented on Thu Jul 06 2017
The largest problem I have encountered so far using the Flutter framework is state management. The documentation only provides examples of handling state in the same component and the parent component, but this is not suitable for a complex application. I came up with the solution of passing a shared state class to each of my pages, but that was unreliable and didn't scale very well as I added more pages. Next I tried using the flutter_flux library, but it appears that I need to store all of the pages variables in a single store because I can't call setState() when I am using this lib.
Is there a better way of doing things? I am accustomed to doing things the 'angular way' where I share state between components by putting it in shared services and utilizing observables.
I am only asking this here because it appears to be the weakest point in the framework, and is hardly discussed on stackoverflow or any other forum.
@eseidelGoogle commented on Thu Jul 06 2017
Historically Flutter has not tried to express an opinion as to ones whole-app architecture, but understandably you are asking (and many others have asked) for a bit more guidance here.
Our existing customers have explored a variety of approaches. It's possible that @mehmetf or @gavindoughtie have recommendations here.
FYI @Hixie
@animeshjain commented on Sat Aug 12 2017
+1 Hello, can anyone please provide recommendations here. I have been kind of stuck myself trying to figure out if there are certain recipes for state management in complex apps.
@branflake2267 commented on Sat Aug 12 2017
One thing I like to keep in mind when thinking about state management is the page lifecycle. So I think of each page as a unit that may share global persistence variables. These variables can retain the state of how I want to express my logic through the UI. This world doesn't have as much structure as the base apis, so it's expressed through my intent. And I have noticed everybody likes to choose their own path. That said, I have some options I like to choose when I want to persist state across multiple pages or the application. I like to keep things simple and express my application state in long term state variables. And I'll persist these in different ways depending on my application.
So overall, I think state management is pretty easy to deal with. It's a matter of how I want to structure my longer term persistence variables which affect my business logic across the application. And this all depends on how I structure authentication, security, and then how my business logic flows. So for me there isn't any reason for state management to be any different for complex large application vs. small instructional applications because state is retained in longer term variables.
Although transferring into Flutter application, what caught me up is changing state in the application and how can I do this. So I'll wire up handlers to listen for changes and fire off setState when the are fired. And then there are async calls, how do you deal with those. I found a FutureBuilder was a fantastic way to wire up a call, and at the end of it calls setState.
So overall I think of it like this. The application's UI reacts to the changes in short and long term variables. The widget pattern and how the UI is rendered is one of the coolest things about flutter.
Best of luck in your state management.
@ColquePaxi commented on Tue Dec 11 2018
For a newbie manage states it's so hard.
I loved Firebase, GoogleSignIn, Auth_Firebase to manage LogIn/LogOut.
Flutter is a wonderful SDK... the learning curve it's fantastic.
But, would be interesting have a best way to manage the return for Future / async (if not yet exist... I'm a newbie).
For example, imagine the WhatsApp interface: how to list de talks when user run the App?
The talks' list is on Firebase - Realtime Database (future / async).
Structure:
TALKS -> UserLoggedEmail -> Talk01 Talk02 Talk03
The problem here is: how to get the UserLoggedEmail by FirebaseAuth (Future/async) before create my TalksPage?
In this code bellow, I don't found a simple way to pass a updated variable to another page when the variable depend Future/async finished. How to do it?
It's works fine after I click on FAB.
This problem can be very simple, but I imagine it's a common use case where need a more examples to desmistify it.
Thanks a lot.
@theankurkedia commented on Wed Jan 31 2018
@ColquePaxi I think you can solve this by using
setState()
insidemostraUID()
when the user is signed in. And it is happening because you are using it insidebuild()
which executes for the first time before completing the async operation. So, you only get the logged-in user when you usesetState()
the next time, which happens when you click on FAB.@ColquePaxi commented on Tue Dec 11 2018
@theankurkedia The step1 (way to pass a updated variable to another page when the variable depend Future/async finished) works fine. Thanks!!! But step2 (how to get the UserLoggedEmail by FirebaseAuth (Future/async) before create my TalksPage?) no...
See new implementation:
See run log:
See that _reference.path: 1) It's null because Widget build is ran before finished mostraUID (therefore my query on FirebaseAnimatedList don't have nothing - no have path - to show me datas). It's the Database error cause. 2) When finishes mostraUID, _reference.path is updated... but how to call Widget built again?
It seems to me that FutureBuilder could be a solution, but I don't know how to implement.
@theankurkedia commented on Thu Feb 01 2018
@ColquePaxi Maybe you can check if the snapshot and its value is null and show a loader if true, otherwise populate the list with the data. You can check this. I have done it this way.
@ColquePaxi commented on Tue Dec 11 2018
@theankurkedia I still can not update the value of the Firebase path. Could you show me how to pass updated conversasDatabaseReference to main.dart? Where's the wrong? Only this is missing to solve.
See new codes:
I used same you:
@ColquePaxi commented on Fri Feb 02 2018
log errors:
@oneplusseven commented on Fri May 11 2018
Meanwhile, this video from Google I/O suggests other possibilities that may help you: https://youtu.be/RS36gBEp8OI.
@filiph commented on Thu Nov 08 2018
Our new site has https://flutter.io/docs/development/data-and-backend/state-mgmt. I will be working on this page and will update this bug as I go along.
Right now, the page is an amalgamation of links, going through different approaches — roughly from the simple to the complex. We'll be adding more guidance. It will always be "it depends" (on the project, on the team, on timeframe, etc.), but I think we can get to a point where starting with Flutter is much easier from the perspective state management.