Flutter: I am not able able fetch data from Firebase

There are four quotes in the firestore but in the output i am not been able to fetch those. Following is the quotesPage and below that quotesController.

fetchQuotes(String catId) {
  print(catId);

  try {
    quotesList.bindStream(firebaseFirestore
        .collection('quotes')
        .where(
          'category',
          isEqualTo: "General",
        )
        .snapshots()
        .map((QuerySnapshot query) {
      List<Quotes> quotes = [];
      for (var quote in query.docs) {
        final quoteModel =
            Quotes.fromDocumentSnapshot(documentSnapshot: quote);
        quotes.add(quoteModel);
      }
      return quotes;
    }));
  } on Exception catch (e) {
    print(e);
  }
}

Upvotes: 0

Views: 38

Answers (1)

Aayush Bhattarai
Aayush Bhattarai

Reputation: 850

You should listen for the snapshot from Firestore.

Try this snippet:

 fetchQuotes(String catId) {
  print(catId);

  try {
    quotesList.bindStream(firebaseFirestore
        .collection('quotes')
        .where(
          'category',
          isEqualTo: "General",
        )
        .snapshots()
        .listen((snapshot) {
      snapshot.map((query) {
        List<Quotes> quotes = [];
    for (var quote in query.docs) {
      final quoteModel =
          Quotes.fromDocumentSnapshot(documentSnapshot: quote);
      quotes.add(quoteModel);
    }
    return quotes;
      });
    }));
  } on Exception catch (e) {
    print(e);
  }
}

Upvotes: 1

Related Questions