gpt4 book ai didi

flutter - 试图将 DocumentID 从 ListView 传递给 ¨ Like ¨ Function

转载 作者:IT王子 更新时间:2023-10-29 07:16:19 24 4
gpt4 key购买 nike

我使用 ListView 来显示我从 Firebase 获得的所有帖子,当用户按下按钮时,它们将特殊帖子的 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”和“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 吗?尝试记录 snapshot.data 以检查它。

编辑以添加代码改变这个

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

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

关于flutter - 试图将 DocumentID 从 ListView 传递给 ¨ Like ¨ Function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57277288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com