gpt4 book ai didi

flutter - 如何在 Flutter 项目中从 Firebase 获取自动生成的 key ?

转载 作者:行者123 更新时间:2023-12-03 04:55:47 29 4
gpt4 key购买 nike

这是这篇文章的续篇:I'd like to add a like button to this flutter blog app

目前我可以使用 print(snap.key); 打印出 Firebase 实时数据库中自动生成的 key 。

我的问题是将它添加到这个 onTap 函数_incrementCounter。不知道在这里做什么。当然如果我更换了+ snap.key +使用任何 firebase 帖子键,则该功能仅适用于该特定帖子:

void _incrementCounter() async {
try {
var ref = FirebaseDatabase.instance.reference().child('posts/'+ snap.key +'/counter');
await ref.once().then((data) async => {
await ref.set(data.value + 1),
});
} catch (e) {
print(e.message);
}
}
}

enter image description here

任何帮助深表感谢。谢谢你们!

最佳答案

您可以将 key 传递给您的函数。还是我误解了什么?

...
onTap: (){
_incrementCounter(postsList[index].key);
}
...

void _incrementCounter(key) async {
try {
var ref = FirebaseDatabase.instance.reference().child('posts/'+ key +'/counter');
await ref.once().then((data) async => {
await ref.set(data.value + 1),
});
} catch (e) {
print(e.message);
}
}
}

关于flutter - 如何在 Flutter 项目中从 Firebase 获取自动生成的 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60618216/

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