1

I was trying to get data from firestore using class defined like this one:

  Future<QuerySnapshot<Map<String, dynamic>>> getPubs() async {
    queryPubs = await pubsDb.get();
    if (queryPubs!.docs.isEmpty) {
      log("[getPubs] Variable queryPubs is empty");
    } else {
      await pubData(queryPubs!.docs).then((value) {
        if (value.isNotEmpty) {
          isPub.value = true;
          log("[getPubs] Variable pubAll is not empty");
        } else {
          log("[getPubs] Variable pubAll is empty");
        }
      });
    }
    return queryPubs!;
  }
  
    Future<List<Map<String, dynamic>>> pubData(List<DocumentSnapshot>? docs) async {
for (int i = 0; i < docs!.length; i++) {
  DbPublisher dbPublisher = DbPublisher.fstoreInfo(docs[i]);
  final imgUrl = await getImgUrl(dbPublisher.imgName, DataType.PUBLISHER);
  pubInfo = {
    "name": dbPublisher.name,
    "imgURL": imgUrl,
    "imgName": dbPublisher.imgName,
    "desc": dbPublisher.desc,
    "email": dbPublisher.email,
    "active": dbPublisher.active,
    "status": dbPublisher.status,
    "albums": dbPublisher.albums,
    "songs": dbPublisher.songs,
    "state": dbPublisher.state,
    "privilege": dbPublisher.privilege,
    "payout": dbPublisher.payout,
    "compensation": dbPublisher.compensation,
    "started": dbPublisher.started,
    "due": dbPublisher.due,
    "license": dbPublisher.license,
    "duration": dbPublisher.duration,
    "valid": dbPublisher.valid,
  };
  pubAll!.add(pubInfo);
}
return pubAll!;

}

I get the data, but when I was about to add one more data; the new data doesn't get displayed. Instead, it was previous data and there were redundant of that. I used the latest package of firestore. Did someone ever experience this one? I used GetX dependency injection for all of this. I used GetxService as it made it as permanent.

Update: redundant here I meant there is the same data over and over. But when I restarted the app, all the data return normal as it used to be. I wonder what is going on?

Update: The flutter code

Expanded(
  child: FutureBuilder<QuerySnapshot<Map<String, dynamic>>>(
    future: dNd.getPubs(),
    builder: (context, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
       if (snapshot.hasData) {
        listAlbs = snapshot.data!.docs;
        if (listAlbs.isNotEmpty) {
          totalAlbs = listAlbs.length;
          log("Load $totalAlbs albums from Firestore");
          return ListView.builder(
            padding: EdgeInsets.zero,
            shrinkWrap: true,
            physics: const ScrollPhysics(),
            controller: scrollCtrl,
            itemCount: listAlbs.length,
            itemBuilder: (context, index) {
              return albsFstore(context, dNd.pubAll![index], index);
            },
          );
        } else {
          return const Center(child: FavText("The album data is empty", 15.0));
        }
      } else if (snapshot.hasError) {
        return const Center(child: FavText("The server is error", 15.0));
      } else {
        return const Center(child: FavText("The data is empty", 15.0));
      }
    } else if (snapshot.connectionState == ConnectionState.waiting) {
     return AlignPositioned(
      dx: 0,
      dy: -(MediaQuery.of(context).size.height / 4),
      child: Container(
        width: 128,
        height: 128,
        padding: const EdgeInsets.all(8.0),
        decoration: const BoxDecoration(borderRadius:     
        BorderRadius.all(Radius.circular(16.0)), color: Color.fromARGB(88, 44, 44, 44)),
        child: const LoadingIndicator(indicatorType: Indicator.ballRotateChase, colors: rainbowColors),),);
    }
    return const SizedBox();
    },
  ),
),
Wege
  • 177
  • 2
  • 11
  • Maybe this is because I set persistence as true? I will come back after investigation. – Wege Oct 14 '22 at 02:50
  • Nah, the data is not updated only after I restarted the app. This is weird. – Wege Oct 14 '22 at 03:48
  • 1
    so were you able to fix this as per your comment above. – Vaidehi Jamankar Oct 14 '22 at 07:02
  • Just updated the question with Flutter code inside. Could you help me out for this @VaidehiJamankar??? – Wege Oct 14 '22 at 07:34
  • Or do you want to be contributor of my project? I will add you if you have github account? You will get paid of course. @VaidehiJamankar??? – Wege Oct 14 '22 at 07:46
  • 1
    I recommend you should check these examples once to see if they help https://stackoverflow.com/questions/68872267/duplicated-data-from-firestore and https://stackoverflow.com/questions/52130316/how-to-get-data-from-firebase-in-flutter. Also go through this useful document for onetime read implementation https://firebase.flutter.dev/docs/firestore/usage/#one-time-read – Vaidehi Jamankar Oct 14 '22 at 12:54
  • Okay, noted. Now I have gotten where I went wrong. I will fix this asap. I have to use another trick to download url image, and that is where I went wrong. I should have used firebase storage for this and saved it along with another firestore data all and at once. – Wege Oct 15 '22 at 04:59
  • Wege, was the information in answer below helpful , do let me know. – Vaidehi Jamankar Oct 26 '22 at 12:15

1 Answers1

1

Cloud Firestore gives you the ability to read the value of a collection or a document. This can be a one-time read, or provided by real time updates when the data within a query changes.
For basic write operations, you can use set() to save data to a specified reference, replacing any existing data at that path and to read a collection or document once, call the Query.get or DocumentReference.get methods.

As recommended in comments, you should check these examples once Duplicate Data from Firestoreand How to get data from Firestore. Also go through this useful document for one time read implementation.

Vaidehi Jamankar
  • 1,232
  • 1
  • 2
  • 10