为什么Firestore数据赋予类"QueryDocumentSnapshot"没有实例方法"[]".错误? [英] Why is Firestore data giving Class 'QueryDocumentSnapshot' has no instance method '[]'. error?
问题描述
我正在尝试从Firestore获取图片,并且出现此错误,数据库中图像的关键是图像1网址"
I am trying to get the image from firestore and it give this error, the key to the image in database is 'image 1 Url'
相同的代码和相同的结构可在我的其他应用程序上运行,但此应用程序会引发此错误
the same Code and same structure works on my other app but this one it throws this error
Class 'QueryDocumentSnapshot' has no instance method '[]'.
Receiver: Instance of 'QueryDocumentSnapshot'
Tried calling: []("image 1 Url")
这是firestore数据库结构,
this is the firestore database structure,
class Toyota extends StatefulWidget {
@override
_ToyotaState createState() => _ToyotaState();
}
class _ToyotaState extends State<Toyota> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder(
stream: FirebaseFirestore.instance.collection('toyota').snapshots(),
builder: (context, snapshot) {
if (snapshot.data == null)
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.red,
valueColor: new AlwaysStoppedAnimation<Color>(Colors.teal),
),
);
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) => SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(3)),
child: Image.network(
snapshot.data.documents[index]['image 1 Url'],
),
),
),
));
},
));
}
}
推荐答案
我发现使用get('String')的解决方案可以与我一起工作
the solution I found to use get('String') this work with me
snapshot.data.documents[index].get('image 1 Url')
您可以查看更多信息: https://developers.google.com/android/reference/com/google/firebase/firestore/QueryDocumentSnapshot
you can check this for more : https://developers.google.com/android/reference/com/google/firebase/firestore/QueryDocumentSnapshot
这篇关于为什么Firestore数据赋予类"QueryDocumentSnapshot"没有实例方法"[]".错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!