Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I listen to Firstore data only from server?

If we want to use .get() method to fetch data from server only, we can use the following:

FirebaseFirestore.instance.collection('users').get(GetOptions(source: Source.server))

Here we used GetOptions(source: Source.server) to avoid reading from cache; but what if I want listen from server only, for example I have the following code:

FirebaseFirestore.instance.collection('userss').doc('1').snapshots()

Or:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

FirebaseFirestore.instance.collection('userss').doc('1').snapshots()listen((event) {  });

I tried the following:

FirebaseFirestore.instance.collection('userss').doc('1').snapshots()listen((event) { 
      if(!event.metadata.isFromCache) {
            print('this data from server');
            //as (!) condition it should outputs true if data was from server  
            // but as a test i turn off my wifi but it also outputs true .. How !??
      }
});

How can I listen from server only? If it’s not possible, how can I at least detect if my data is from server or not?

>Solution :

There is no way with a listener to not get any cached data, but you can check in each snapshot’s metadata whether it is guaranteed to be up to date with the server by checking its fromCache property. From that link:

True if the snapshot was created from cached data rather than guaranteed up-to-date server data. If your listener has opted into metadata updates (via SnapshotListenOptions) you will receive another snapshot with fromCache set to false once the client has received up-to-date data from the backend.

If the property is false, the data is guaranteed to be up to date from the server.


The alternative would be to disable disk caching.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading