skip to Main Content
  static List categoryList() {
    final categorySnapshots = FirebaseFirestore.instance
        .collection('categories')
        .orderBy('name')
        .snapshots();

    List categories = [];
    categorySnapshots.map((snapshot) => snapshot.docs.map((doc) {
          print(snapshot.toString());
          categories.add(doc.data()['name']);
        }));

    print(categories);
    return categories;
  }

Categories is empty.
How to populate it with the data from snapshots?

2

Answers


  1. Using the below code might help

    you can convert the snapshot to Map<String,dynamic> by using the following function:

     static Post fromSnap(DocumentSnapshot snap) {
    var snapshot = snap.data() as Map<String, dynamic>; 
    }
    
    Login or Signup to reply.
  2. I added a new collection called "school", there’re two items added inside the document.

      void getMessagesTest() async{
        QuerySnapshot querySnapshot = await _firestore.collection('school').orderBy('age',descending: true).get();
        final allData = querySnapshot.docs.map((doc) => doc.data()).toList();
        print(allData);
      }
    
    

    I used my code, and it works. Could you please remove ".where" and try it again?

    enter image description here
    enter image description here

    You could chain where and orderBy together. Please see my code below. Reference link => Using Where and Order by different fields in Firestore query

      void getMessagesTest() async{
        QuerySnapshot querySnapshot = await _firestore.collection('school').orderBy('age', descending: true).where('age', isGreaterThan: 17).get();
        final allData = querySnapshot.docs.map((doc) => doc.data()).toList();
        print(allData);
      }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search