EK5
EK5

Reputation: 112

Does Streambuilder store the data after app restarts?

I have a simple streambuilder that reads the users document, and I use it to show some of the user's data. My question is, would this streambuilder re-read the document everytime the user restarts the app? If, yes is there any way to prevent the streambuilder from re-reading it everytime the user restarts the app unless there is a change in the document?

StreamBuilder(
  stream: _firestore
      .collection('users')
      .doc(_auth.currentUser!.uid)
      .snapshots(),
  builder:
      (context, AsyncSnapshot<DocumentSnapshot<Object?>> snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return CircularProgressIndicator(
        color: isDarkMode ? Colors.white : Colors.black,
      );
    }
    if (snapshot.hasData) {
      if (snapshot.data!.exists) {
        snapshot.data!['serviceEnabled'] == true
            ? startServices()
            : null;
        return Center(
          child: Column(

This streambuilder is on the homepage of the app, I show some of the user's data on the homepage.

Upvotes: 0

Views: 30

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599001

How would the database know whether there's a change in the document without reading that document?

If you can answer that, you can probably write a query to match that same condition.

For example, if each document has a lastUpdated field, you could just get the updated document with:

_firestore
  .collection('users')
  .where('lastUpdated', '>', timestampWhenYouLastReadDocuments)
  .get()

Aside from that query to update the cache, you could then get the documents from the cache in other places in your app.

Upvotes: 1

Related Questions