i want to read data from firestore in a listtile i dont want to use streambuilder i want to access the documents field individually i try this but not working
class ProductList extends StatelessWidget {
Stream<DocumentSnapshot> snapshot = Firestore.instance.collection("listofprods").document('ac1').snapshots();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black)
),
child: ListTile(
title: Text(snapshot['name']),//here error
),
)
],
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!
We built a simple Flutter application that uses the Firestore database as a backend. You also learned how to perform the four most important tasks when working with a database: creating data, updating data, reading data, and deleting data. From now on, you can make more complicated and complex apps with that tech stack.
This works in FlutterFire! I used DocumentSnapshot and await. By using DocumentSnapshot, you can get the document including document fields and its values. A DocumentSnapshot contains data read from a document in your Cloud Firestore database.
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 realtime updates when the data within a query changes. To read a collection or document once, call the Query.get or DocumentReference.get methods.
As Per 2021 :
streamSnapshot.data.docs
Documents Changes to doc .
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('chats/XYSDa16jZBO5CUMwIk0h/messages')
.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
return ListView.builder(
itemCount: streamSnapshot.data.docs.length,
itemBuilder: (ctx, index) =>
Text(streamSnapshot.data.docs[index]['text']),
);
},
));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With