firebase - flutter : Cannot display image from image url firestore

标签 firebase dart flutter google-cloud-firestore

我尝试使用图像 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/

相关文章:

java - Firebase数据库如何从1个引用中获取一些值并等于2个引用来获取其键和值

iOS - Firebase 错误 : Use of unresolved identifier FIRApp running supplied code

typescript - Firebase 和 RxJS

dart - 如何在执行 POST 请求时解决 Flutter CERTIFICATE_VERIFY_FAILED 错误?

flutter - 在空方法上调用了 'call'方法。接收方:null尝试过的调用:call(navegacion_bloques)

java - 在嵌套方法中更新变量不会全局更新它

flutter - 为什么 Visual Studio Code 中的代码文本没有颜色?

android - D8 : Cannot fit requested classes in a single dex file

android - flutter 中的 FCM 自定义通知图标

save - 如何在 Flutter 中一开始就加载主题