Sermed mayi
Sermed mayi

Reputation: 757

Flutter + Firebase : Class 'QuerySnapshot' has no instance getter 'documents'

I searched in topics with same problem but no one solve mine.

I have this cloud store :

enter image description here

I create this service :

import 'package:cloud_firestore/cloud_firestore.dart';

class FireBaseApi {
  getData() {
    try {
      var databse = FirebaseFirestore.instance.collection('mycollection');
      return databse;
    } catch (e) {
      print(e);
      return null;
    }
  }
}

and used it like this :

   return StreamBuilder(
          stream: FireBaseApi().getData().snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Scaffold(
                body: Center(
                    child: SizedBox(
                        width: 100,
                        height: 100,
                        child: CircularProgressIndicator())),
              );
            } else {
              print('length = ${snapshot.data.documents}');
              return SafeArea(
                child: Scaffold(
                    body: ListView( children: [Text('Noj Clinic'),
child: GridView.builder(
            itemCount: snapshot.data.size, .......etc
)],)

I gave error Class 'QuerySnapshot' has no instance getter 'documents'.

and this is shown :

enter image description here

Upvotes: 0

Views: 411

Answers (1)

Victor Eronmosele
Victor Eronmosele

Reputation: 7686

Change documents to docs. That should fix it.

So this

print('length = ${snapshot.data.documents}');

should become:

      print('length = ${snapshot.data.docs}');

https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/QuerySnapshot/docs.html

Upvotes: 2

Related Questions