我尝试使用图像 url 显示来自 firestore 的数据。我使用 FutureBuilder 从 firestore 获取我的图像 url 值。但我有问题,因为我的图像无法显示并显示红屏。我在调试控制台中收到如下消息:
Another exception was thrown: A build function returned null.
I/flutter (13506): Another exception was thrown: NoSuchMethodError: The method '[]' was called on null.
I/flutter (13506): Another exception was thrown: NoSuchMethodError: Class 'QuerySnapshot' has no instance method '[]'
这是我的代码:
ListTile(
leading: new FutureBuilder(
future: Firestore.instance.collection('users').where('uid', isEqualTo: _post['imgurl']).getDocuments(),
builder: (BuildContext context, AsyncSnapshot snapshot){
if(snapshot.hasData){
if(snapshot.data != null){
print(snapshot.data['imgurl']);
return CircleAvatar(
backgroundImage: NetworkImage(snapshot.data['imgurl']),
);
}
}
},
),
title: Text('Richard');
)
你能告诉我我的错在哪里吗?
最佳答案
您的 Future
返回文档列表,而不仅仅是一个。您正在尝试从 document
列表中获取文档的 imageUrl
,而不是从一个列表中获取。
您的 snapshot.data
是一个文档列表,您可以在 snapshot.data.documents
中找到它们。
如果您只对一个文档感兴趣,我建议您将 Future
更改为仅返回一个文档的内容(例如获取第一个文档)。
如果你想保持你的 Future
不变,只需从你的 snapshot.data
中获取第一个文档并对其进行处理。
目前我无法提供更多示例,因为我无法从逻辑上做到这一点(我不在家),但如果我的建议不能帮助您解决问题,我可以为您提供一些示例!
关于firebase - flutter : Cannot display image from image url firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54683705/