How to get all data from a Firestore collection in flutter?

3 min read
get all data from a Firestore collection in flutter
get all data from a Firestore collection in flutter

Earlier we have been through articles related to firebase like How to Delete All Documents In a Collection In FireStore With Flutter? and many more. So today, in this article, we will learn about How to get all data from a Firestore collection in flutter.

How to get all data from a Firestore collection in flutter?

Call the getDocs() function, then use the build function, then print all the document IDs in the console. Here is how you do it!

Future getDocs() async {
  QuerySnapshot querySnapshot = await Firestore.instance.collection("collection").getDocuments();
  for (int i = 0; i < querySnapshot.documents.length; i++) {
    var a = querySnapshot.documents[i];
    print(a.documentID);
  }
}

Get all data from the collection that you found working, without using deprecated methods.

CollectionReference _collectionRef =
    FirebaseFirestore.instance.collection('collection');

Future<void> getData() async {
    // Get docs from collection reference
    QuerySnapshot querySnapshot = await _collectionRef.get();

    // Get data from docs and convert map to List
    final allData = querySnapshot.docs.map((doc) => doc.data()).toList();

    print(allData);
}

Using StreamBuilder as shown below:

import 'package:flutter/material.dart';
import 'package:firebase_firestore/firebase_firestore.dart';

class ExpenseList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new StreamBuilder<QuerySnapshot>(
        stream: Firestore.instance.collection("expenses").snapshots,
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (!snapshot.hasData) return new Text("There is no expense");
          return new ListView(children: getExpenseItems(snapshot));
        });
  }

  getExpenseItems(AsyncSnapshot<QuerySnapshot> snapshot) {
    return snapshot.data.documents
        .map((doc) => new ListTile(title: new Text(doc["name"]), subtitle: new Text(doc["amount"].toString())))
        .toList();
  }
}

User can also give try the following code and check the cloud_firestore plugin for flutter :

QuerySnapshot snap = await 
    Firestore.instance.collection('collection').getDocuments();

snap.documents.forEach((document) {
    print(document.documentID);
  });

Conclusion:

Thanks for being with us on a聽Flutter Journey!!!

If you are still having doubts about Flutter Development, Let us know!! We would love to assist you 馃檪

So in this article, we have Learned How to get all data from a Firestore collection in flutter!!

Keep聽Learning聽!!! Keep聽Fluttering !!! Stay Connected !!!

Also, check out the complete step-by-step guide on Implement Staggered GridView In Flutter!

Don’t forget to drop your valuable feedback in the comments right below 馃檪

Flutter Agency聽is our portal Platform dedicated to Flutter Technology and聽Flutter Developers. The portal is full of cool resources from Flutter like聽Flutter Widget聽Guide,聽Flutter Projects,聽Code libs聽and etc.

Flutter Agency聽is one of the most popular online portals dedicated to聽Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of聽Flutter.

Leave a Reply