0

i am trying to save data reads which have been not changed yet to avoid more and more the same repeated data that not changed yet ..

i have normal Future.Builder that get data from firstore (network side)

    Widget build(BuildContext context) {
        return FutureBuilder(
          future: FirebaseFirestore.instance.collection('users').get(),
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {


    if (!snapshot.hasData) {
    return const Expanded(child: SizedBox()) ;
    }
      return ListView.builder(
      
    itemCount: snapshot.data!.docs.length ,
    itemBuilder: (context, int index) {
    DocumentSnapshot documentSnapshot = snapshot.data!.docs[index];
   
    return ListView.builder(
    itemCount: snapshot.data!.docs.length ,
    itemBuilder: (context, int index) {
    DocumentSnapshot documentSnapshot = snapshot.data!.docs[index];
    return Text(documentSnapshot['products'])
    }
    );
    
  }
}

and i have into every single document Timestamp and i need to use where('modify',isGreaterThen : HERE i need to put the old timestamp from cashe to chick if it not changed yet to decide to fetch the new ones

in flutter i cannot handle it as well .. How can i fetch the cashed data with the new ones from network in the harmonic index such as reading the whole data in normal way .. so i avoided these old ones to be reload again ..

i have read a lot of this topic but it was in old Firestore version also it was using java code ...

this following code that cannot handle in flutter

 Source CACHE = Source.CACHE;
    Source SERVER = Source.SERVER;
    Query.Direction DESCENDING = Query.Direction.DESCENDING;
    FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
    CollectionReference shoesRef = rootRef.collection("shoes");
    Query lastAddedQuery = shoesRef.orderBy("lastModified", DESCENDING)
    shoesRef.get(CACHE).addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            boolean isEmpty = task.getResult().isEmpty();
            if (isEmpty) {
                shoesRef.get(SERVER).addOnCompleteListener(/* ... */);
            }
        }
    });

Query query = shoesRef.orderBy("lastModified", DESCENDING)
                      .whereGreaterThan("lastModified", savedDate);

source code was written by Alex Mamo

https://medium.com/firebase-tips-tricks/how-to-drastically-reduce-the-number-of-reads-when-no-documents-are-changed-in-firestore-8760e2f25e9e

any support or example with latest version of Firbase and in dart or flutter code will be so thankful ..

best regards

Mohammed Hamdan
  • 1,006
  • 5
  • 23
  • There should be no significant differences in *capabilities* between the Flutter API for Firestore and the the Android one. So the approach from your previous code should be portable pretty easily. If you're having trouble mapping a specific part of the old code, please edit your question to show us the Java code and we may be able to help. – Frank van Puffelen Jan 06 '22 at 21:38
  • thanks , i have edit my question ,, please take a look – Mohammed Hamdan Jan 07 '22 at 07:54
  • Given where you got the approach from, let's see if @AlexMamo can have a look. :) – Frank van Puffelen Jan 07 '22 at 16:25

0 Answers0