Flutter + Firebase : Class 'QuerySnapshot' 没有实例 getter 'documents'

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

我搜索了有相同问题的主题,但没有人解决我的问题。

我有这个云商店:

我创建了这个服务:

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;
    }
  }
}

并像这样使用它:

   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
)],)

我报错了Class 'QuerySnapshot' has no instance getter 'documents'.

显示如下:

documents 更改为 docs。那应该可以解决它。

所以这个

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

应该变成:

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

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