Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read data from firestore flutter

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
                ),
              )
            ],
like image 261
Lalit Rawat Avatar asked Dec 30 '19 10:12

Lalit Rawat


People also ask

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

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!

What can you do with a flutter database?

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.

How to get document fields and its values in flutterfire?

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.

How do I read the value of a collection in FireStore?

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.


1 Answers

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']),
            );
          },
        ));

  }
like image 197
Tasnuva Tavasum oshin Avatar answered Nov 15 '22 09:11

Tasnuva Tavasum oshin