Reputation: 359
I have a Flutter app that works very similar to twitter, there is a homescreen with a home feed that creates a list of recursive posts. The situation I see however is the following: a user can tap on a post, and query ten document reads into the state of a stateful widget. Then, the user can call Navigation.pop to leave the screen and dispose the widget. However, the user can then enter the same post, and query ten more reads of identical data. This has the capacity to go on forever. My question is, is there any way to cache or prevent this situation from occurring?
Thanks!
Upvotes: 0
Views: 37
Reputation: 9166
I would make it this way, first, create a class that will be responsible for caching the response got from Firestore, then declare a static
member called documents as example, it will be a List<Map<String, dynamic>>
.
class DataClass {
static List documents = [];
}
Now when you fetch documents from Firestore, you can save those documents for future use in your app:
DataClass.documents.addAll(querySnapshot.docs.map((doc) => doc.data() as Map<String, dynamic> ).toList());
Now, in your UI, instead of making requests every time using the get()
, you can build your Ui based on that DataClass.documents
list which contains your documents data.
If you need to retrieve more data, for example, you can make a call that gets you the response of more documents and add theme with same operation.
Upvotes: 1