itemcount 中的 snapshot.data.length 不起作用:“在 null 上调用了 getter 'length'”

ban*_*ang 6 dart flutter google-cloud-firestore

我正在尝试使用以下代码从 firestore 获取文档:

\n\n
 Future getCategories() async {\n    var firestore = Firestore.instance;\n    QuerySnapshot qn = await firestore.collection("categories").getDocuments();\n    return qn.documents;\n  }\n\n @override\n  Widget build(BuildContext context) {\n    return Container(\n      child:FutureBuilder(\n        future:getCategories(),\n        builder:(context, snapshot){\n          if(snapshot.connectionState == ConnectionState.waiting){\n            return Center(\n              child:Text("Loading...")\n            );\n         }\n         else\n         {\n           return GridView.builder(\n             itemCount: snapshot.data.length,\n             gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(\n                 crossAxisSpacing: 6.0, mainAxisSpacing: 6.0, crossAxisCount: 2),\n              itemBuilder: (BuildContext context, int index) {\n                return SingleCategory(\n                  category_name:  snapshot.data[index].data["title"],\n                  category_picture: snapshot.data[index].data["picture"],\n                );\n              }\n           );\n         }\n        }\n      )\n    );\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我运行代码时,出现以下错误:

\n\n
\n

I/flutter ( 7555): \xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1 小部件库捕获异常\n \xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90 \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2 \x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95 \x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90 \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2 \x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95 \x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90 \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90 I/颤振\n(7555) : 构建时抛出以下 NoSuchMethodError\n FutureBuilder(dirty, state: I/flutter ( 7555):\n _FutureBuilderState#c3e7b): I/flutter ( 7555): getter \'length\' 在 null 上调用。I/flutter ( 7555): 接收器: null\n I/flutter ( 7555): 尝试调用: length I/flutter ( 7555): I/flutter\n ( 7555): 抛出异常时,这是堆栈: I/flutter\n (7555): #0 Object.noSuchMethod\n (dart:core/runtime/libobject_patch.dart:50:5)

\n
\n\n

任何人都可以帮助我吗?

\n

小智 5

尝试这个 :

future: getData(),
builder: (context, AsyncSnapshot<List<User>> snapshot)
Run Code Online (Sandbox Code Playgroud)


Kir*_*hov 2

正如我们在评论中发现的,您正在使用身份验证规则,该规则拒绝所有请求的访问:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
       allow read, write: if false;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我想你想写这样的东西(只读模式):

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
       allow read;
       allow write: if false;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

试试这个规则