NoSuchMethodError:类“ QuerySnapshot”没有实例方法“ []”

时间:2020-06-28 12:39:57

标签: firebase flutter dart

我正在尝试获取每个用户的帖子(图像)。用户ID存储在posts集合中,每个userid文档将有一个子集合,其中包含所有posts文档。但是我收到此错误。

The following NoSuchMethodError was thrown building:
Class 'QuerySnapshot' has no instance method '[]'.
Receiver: Instance of 'QuerySnapshot'
Tried calling: []("mediaUrl")

这是代码。

getPost(){
    return FutureBuilder(
        future:  Firestore.instance.collection('posts').document(user.id
        ).collection('userPosts').getDocuments(),

        // ignore: missing_return
        builder: (context,snapshot) {
          if (snapshot.hasData) {
            return
              Container(
              height: 200,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[ListView.builder(
              shrinkWrap: true,
                  scrollDirection: Axis.horizontal,
                  itemCount: snapshot.data.documents.length,
                  itemBuilder: (context, index) {
                       
                        child: Image.network(snapshot.data["mediaUrl"],)
                    );
                  }
              ),
          ],
              ),
            );


          }
          if (!snapshot.data ) {
            return Text('No posts');
          } else {
            return CircularProgressIndicator();
          }
        }
    );
  }`

1 个答案:

答案 0 :(得分:1)

如果您的媒体网址位于文档中,请尝试以下操作:

username,password and email

使用另一种方法的完整代码:

Image.network(snapshot.data.documents[index]["mediaUrl"],)