In my page I have many FutureBuilder that share the same data from a future. As the future is a request from Firebase, I just want to do it one time. Do you have a solution ?
Whenever you FutureBuilder calls the future, you can store its value inside a variable.
After that, you can pass that variable in your widgets via the constructor or if you are in the same stateful widget then you can directly assign it via setstate method.
Related
When should I use future builder or future provider. It seems to me they do almost same work.
The answer is from here:
FutureProvider: you can use this to retrieve a document from
firestore and show a loading screen while waiting, but can't make
changes to it afterwards, that will also show in the UI
FutureBuilder: same as above but it can't provide data
down the widget-tree
I was wondering what is the better option to get documents from firestore.
Should i run the getdocuments in initstate or should I run it as a future builder.
So far these are the pros and cons i have seen.
initstate - if I were to retrieve it initState, then everytime i click on a post to see its details, and when i go back, the initstate runs again, creating more documents read. However, this does help refresh certain values, incase of a user bookmarking a post, that way the bookmark value gets updated, however in the cost of more documents read.
futurebuilder - seems cost efficient in the sense that, when i click on a post and go back, no more documents are retrieved from firestore, however, if i were to bookmark the post in the postdetails page, the value does not get updated. However to work this around, i could just remove the bookmark value in the Post page.
I have however, used the getdocuments in the initstate when i go into the postdetails page by deseralizing it.
Please advise.
Calling getDocuments() is deprecated for get() as per the change log.
I would do one of the following:
If using initState, the easiest way to deal with this is to store a
Future variable in your state. In initState make the API call and
store the Future. You can then pass this Future to FutureBuilder.
Keep using FutureBuilder, then when you edit the data make an async
operation to update the database and after the database is updated
call setState to reflect the changes on the UI.
Use a StreamBuilder, which is similar to FutureBuilder, but instead
of building only once, it rebuilds each time there's a change on the
stream it's attached to. This way you just need to update the
datasource and the widget will update itself with the new data.
So my answer would be it depends on your use case. If your data updates every min/sec while you use the app, it needs updates asynchronously at a certain interval, in that case StreamBuilder is the best option but If your use case is to just get the data, and display it, then you can use FutureBuilder.
I'm somewhat new to flutter and my question is more philosophical. When using setState(), is it bad practice to call it without using new data in the method? Ex: I call it like this:
setState({});
If there's a better way to refresh the build function or is this the best way?
setState notifies that internal state of the object is changed, that reflect on entire subtree.
If you just change the state directly without calling setState, the framework might not schedule a build and the user interface for this subtree might not be updated to reflect the new state.
There are some other ways to trigger build method when data changes, but easiest one is setState() and for your purpose it is best.
No need to use new data in your case. Your way of doing this is exactly how the Flutter team intended it to be used.
I want to load some data from an SQLite database in my Flutter application and show the results in a ListView. I am currently using a FutureBuilder widget to fetch the data asynchronously from the database and then build the ListView.
However, if I want to do some operations on the data - e.g. modify some information and save it to database - and then show the updated data in the ListView, I think I have to store the data in a local variable first and then call the setState() method to make changes to the data and re-build the page.
Is there any other better/preferred way to achieve the same without using FutureBuilder?
initState gets called when your widget gets initialised for the first time. There you do some initialisation operations.
I think you're right to use FutureBuilder, since your UI gets generated after fetching the data from the Database.
I would do one of the following:
Keep using FutureBuilder, then when you edit the data make an async operation to update the database and after the database is updated call setState to reflect the changes on the UI.
Use a StreamBuilder, which is similar to FutureBuilder, but instead of building only once, it rebuilds each time there's a change on the stream it's attached to. This way you just need to update the datasource and the widget will update itself with the new data.
Talking about the second solution, I cannot go any further since I've never implemented a Stream that would allow to do this (I've always used StreamBuilder it with Firebase streams).
The first solution allows you to update the displayed data without asking again for the entire data set to the database.
I might appreciate some feedback from others regarding the second solution, since I'm not sure about how StreamBuilder manages a change of data.
I'll let you know if I discover more!
https://stackoverflow.com/a/52021385/11252673
build() might be called several times - that's why this kind of initialization goes better on initState.
I understand that ideally if you want to pass around some value like user profile, InheritedWidget comes into play.
But since InheritedWidget can't be accessed from initState (I got error).
If you need to access user profile in initState, how should I handle this?
Should I create static variable, SharedPreferences or any other way?
Is there a way to do this with InheritedWidget?