[英]How do I get the key of my most recent push ID from Firebase Realtime Database in Flutter?
I am trying to get the most recent ID of from my orders table to that I can then use this ID in a path to get the order number and increment it by one.我正在尝试从我的订单表中获取最新的 ID,然后我可以在路径中使用此 ID 来获取订单号并将其加一。
void _activateListeners() {
final recentOrder = _readRef.child('Orders').limitToLast(1).get();
final readOrder = _readRef
.child('Orders/{$recentOrder}/orderNum')
.onValue
.listen((event) {
final orderNum = event.snapshot.value;
setState(() {
lastOrderNum = orderNum.toString();
intOrderNum = int.parse(lastOrderNum) + 1;
newOrderNum = intOrderNum.toString();
});
});
}
I know that I need to use the "key" word somehow but I have been unable to solve this problem.我知道我需要以某种方式使用“关键”词,但我一直无法解决这个问题。
my database table looks as follows:我的数据库表如下所示:
Orders
-N3ZdY6LOL_9Z-6KXHnK
*cartProduct
*dateTime:"2022-06-02 15:41:20.470139"
*orderNum:"6"
*totalAmount:45
-N3ZdgEQIzsjLA5NCu3U
*cartProduct
*dateTime:"2022-06-02 15:41:20.470139"
*orderNum:"7"
*totalAmount:45
Edit: can someone tell me if it is even possible to fetch the unique push ID after it has been made (not directly after the order is added)?编辑:有人可以告诉我是否有可能在完成后(而不是直接在添加订单后)获取唯一的推送 ID? I have looked at all the documentation and I have failed to implement anything.我查看了所有文档,但没有实现任何东西。 I have also found numerous similar questions that are either just different or unanswered.我还发现了许多类似的问题,这些问题要么不同,要么没有答案。
If I know SQL is SQLite a better alternative?如果我知道 SQL 是 SQLite 更好的选择?
You can get the key from the snapshot by using the snapshot's key property, like in this line below:您可以使用快照的key属性从快照中获取密钥,如下面的这一行:
String? eventKey = event.snapshot.key;
Check out the documentation for Flutter's Realtime Database's DataSnapshot class for more information.查看 Flutter 的实时数据库的DataSnapshot类的文档以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.