Flutter 试图将DocumentID从ListView传递到¨;像¨;作用
我使用ListView显示我从Firebase获得的所有帖子,当用户按下按钮时,他们会将一篇特殊帖子的DocumentID发送到一个“like”函数,但我收到以下错误:Flutter 试图将DocumentID从ListView传递到¨;像¨;作用,flutter,dart,Flutter,Dart,我使用ListView显示我从Firebase获得的所有帖子,当用户按下按钮时,他们会将一篇特殊帖子的DocumentID发送到一个“like”函数,但我收到以下错误: I/flutter (28036): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════ I/flutter (28036): The following NoSuchMetho
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.
新建StreamBuilder(
流:db
.collection('消息')
.orderBy('timestamp',降序:true)
.snapshots(),
生成器:(上下文,快照){
if(snapshot.hasData){
返回列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
容器(
高度:400.0,
子项:ListView.builder(
…它正在工作,但我不知道如何从中获取documentID。
您确定在快照.数据中有文档ID。
尝试记录snapshot.data以检查它
编辑以添加代码
改变这个
onPressed: () => 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)});
}
Plz,你能解释一下我是如何记录它的吗?我在使用VS代码开发人员库中有一个log()方法,只需执行
log(snapshot.data.toString())
onPressed: (){
likes(snapshot.data.documents[position].documentID);
}
void likes(documentid) async {
await db
.collection('messages')
.document(documentid)
.updateData({'likes': FieldValue.increment(1)});
}