Dani MHM
Dani MHM

Reputation: 39

The getter 'documents' isn't defined for the type 'QuerySnapshot'

Trying to access data from Firestore real-time database, but I get this message all the time and other couple messages. how can I access the data in this situation? Thank you in advance

class HomePage extends StatefulWidget {
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      FirestoreCrud firestoreCrudRef = new FirestoreCrud();
      Stream<QuerysnapShop> blogsSnapShot;
    
      @override
      void initState() {
        firestoreCrudRef.readData().then((result) {
          blogsSnapShot = result;
        });
        super.initState();
      }
    
      Widget blogList() {
        return Container(
          child: StreamBuilder<QuerysnapShop>(
              stream: blogsSnapShot,
              builder: (context, snapshot) {
                return ListView.builder(
                  scrollDirection: Axis.vertical,
                  physics: ScrollPhysics(),
                  padding: EdgeInsets.only(top: 24),
                  itemCount: snapshot.data.documents.length,
                  shrinkWrap: true,
                  itemBuilder: (context, index) {
                    return BlogTile(
                      author: snapshot.data.documents[index].data()['author'],
                      title: snapshot.data.documents[index].data()['tile'],
                      body: snapshot.data.documents[index].data()['body'],
                      imageUrl: snapshot.data.documents[index].data()['imageUrl'],
                    );
                  },
                );
              }),
        );
      }

Upvotes: 3

Views: 12051

Answers (4)

Montekar
Montekar

Reputation: 164

My problem was that I was using

StreamBuilder()

and had to change to

StreamBuilder<QuerySnapshot>()

and then use

.docs

instead of

.documents

Upvotes: 4

Aayan Agarwal
Aayan Agarwal

Reputation: 49

The solution by preet shah wont work anymore. The below worked for me:

 return ListView.builder(
              itemCount: snapshot.data.docs.length,
              shrinkWrap: true,
              itemBuilder: (context, index) {
                var temp = snapshot.data.docs[index];
                return Container(
                  padding: EdgeInsets.all(10.0),
                  child: Card(
                    child: Text(temp['full_name']),
                  ),
                );
              },
            );

Upvotes: 1

Preet Shah
Preet Shah

Reputation: 1037

The below code should help you achieve the desired result.

ListView.builder(
  scrollDirection: Axis.vertical,
  physics: ScrollPhysics(),
  padding: EdgeInsets.only(top: 24),
  itemCount: snapshot.data.docs.length,
  shrinkWrap: true,
  itemBuilder: (context, index) {
    var temp = snapshot.data.docs[index].data();
    return BlogTile(
      author: temp['author'],
      title: temp['tile'],
      body: temp['body'],
      imageUrl: temp['imageUrl'],
    );
  },
);

Upvotes: 3

pedro pimont
pedro pimont

Reputation: 3084

The QuerySnapshot class has a docs property, which then return a List<QueryDocumentSnapshot>. You can then access an index from this List which gives you a QueryDocumentSnapshot that you can then acess its .data() method to have a Map<String, dynamic>. Finally you can access any field from that.

Try:

snapshot.data.docs[index].data()['imageUrl'],

Upvotes: 5

Related Questions