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?
Related
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.
I have an application where I display a list of cards that represent a model (Let's say a Person). These are stateless widgets as they cannot be changed directly. These instances are obtained from an API and, since the amount of items is large, I am using AutomaticKeepAliveClientMixin to keep them from reloading and triggering more API calls.
When clicked, these cards lead to the Person's page where there we can edit some information on them. Using a StatefullWidget here with setState takes care of all issues related to the page. However, when I pop the individual page the card that was clicked does not reflect the changes made.
I was thinking of writing a ChangeNotifier wrapper for each Person but from what I've read this is a bad idea. Using more powerful state management tools like BLoC or Redux seems like overkill too. How can I implement this properly?
I think you should try:
ontap: () async {
await Navigator.push ....);
ref
}
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 new to Flutter, and I've stumbled upon a problem I need advice in how to approach it. I'm building an app which uses navigator and flutter_bloc. Now when I navigate to a certain route I have the bloc state pull some data from an API and show it. What I want to achieve is not call the API every time when I navigate to that route unless the app has been closed previously or a "pull to refresh" action has been made. How do I got about storing the data after the initial API call. I've tried with the hydrated_bloc but either I don't understand the point of the hydrated_bloc well or it's intention is not to do that. Thank you for the advice in advance.
class AppState {
static final AppState _instance = AppState._internal();
factory AppState() => _instance;
ApiResponseCustomClass apiResponse;
AppState._internal() {
// init things here if needed
}
}
A simple way is to just make a state class where you will keep the result in, and then access it via AppState().apiResponse anywhere in your code.
You can use local_storage to maintain your data locally. So every time you need to fetch data from API again and again. and you can clear data before closing the app.
I am new to flutter and I have been reading about initstate but it's not clear to me how to use and if it's the best solution for what I am trying. There might be other best options I am sure, but I am after some best practices.
I have an specific problem within application that performs some social login and other activities. When I start the application I want to validate if the user is logged, then, if the user is logged return the user to initstate (not sure if that's possible) in order to retrieve more information about that user (i.e if that user has any notifications, etc...) within the initstate function.
I am using initstate within the splashscreen function, so I want to retrieve as much information I can (within the 3s splashscreen best practices ) in order to make the rest of the app smoother. But not sure if it's it's the best way to do it
thanks