尝试将文档 ID 从列表视图传递到 ̈ 喜欢 ̈ 函数



im使用listView显示我从firebase中获得的所有帖子,然后当用户按按钮时,他们将eppecial帖子的documentid发送到'like''以下错误:

I/flutter (28036): ══╡ EXCEPTION CAUGHT BY GESTURE 
╞═══════════════════════════════════════════════════════════════════
I/flutter (28036): The following NoSuchMethodError was thrown while handling a gesture:
I/flutter (28036): Class 'QuerySnapshot' has no instance getter 'documentId'.
I/flutter (28036): Receiver: Instance of 'QuerySnapshot'
I/flutter (28036): Tried calling: documentId

已经尝试更改为" documentid"," documentid"one_answers" documentid"

这是我的代码:

onPressed: () => likes(                                                     
  snapshot.data.documentId),

这是我的函数代码:

void likes(String documentid) async {
    await db
        .collection('messages')
        .document(documentid)
        .updateData({'likes': FieldValue.increment(1)});
  }

这就是我构建listView的方式:

new StreamBuilder(
          stream: db
              .collection('messages')
              .orderBy('timestamp', descending: true)
              .snapshots(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    height: 400.0,
                    child: ListView.builder(
... its working, but i dont know how to get the documentID from it.

您确定 snapshot.data 中的 documentID 吗?尝试记录快照.data检查它。

编辑以添加代码更改此

onPressed: () => likes(                                                     
  snapshot.data.documentId),

to

onPressed: ()
{
  print(snapshot.data.toString());
  likes(snapshot.data.documentId);
}

这是我为纠正错误所做的:

onPressed: (){
likes(snapshot.data.documents[position].documentID);
                                                  }

void likes(documentid) async {
await db
    .collection('messages')
    .document(documentid)
    .updateData({'likes': FieldValue.increment(1)});
  }

最新更新