gpt4 book ai didi

dart - itemcount 中的 snapshot.data.length 不起作用 : "The getter ' length' was called on null"

转载 作者:IT王子 更新时间:2023-10-29 06:58:49 26 4
gpt4 key购买 nike

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

 Future getCategories() async {
var firestore = Firestore.instance;
QuerySnapshot qn = await firestore.collection("categories").getDocuments();
return qn.documents;
}

@override
Widget build(BuildContext context) {
return Container(
child:FutureBuilder(
future:getCategories(),
builder:(context, snapshot){
if(snapshot.connectionState == ConnectionState.waiting){
return Center(
child:Text("Loading...")
);
}
else
{
return GridView.builder(
itemCount: snapshot.data.length,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisSpacing: 6.0, mainAxisSpacing: 6.0, crossAxisCount: 2),
itemBuilder: (BuildContext context, int index) {
return SingleCategory(
category_name: snapshot.data[index].data["title"],
category_picture: snapshot.data[index].data["picture"],
);
}
);
}
}
)
);

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

I/flutter ( 7555): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter ( 7555): The following NoSuchMethodError was thrown building FutureBuilder(dirty, state: I/flutter ( 7555): _FutureBuilderState#c3e7b): I/flutter ( 7555): The getter 'length' was called on null. I/flutter ( 7555): Receiver: null I/flutter ( 7555): Tried calling: length I/flutter ( 7555): I/flutter ( 7555): When the exception was thrown, this was the stack: I/flutter ( 7555): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)

谁能帮帮我。

最佳答案

试试这个:

 future: getData(),
builder: (context, AsyncSnapshot<List<User>> snapshot)

关于dart - itemcount 中的 snapshot.data.length 不起作用 : "The getter ' length' was called on null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54345910/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com