简体   繁体   English

如何从 Flutter 中的 Firebase 实时数据库中获取我最近推送 ID 的密钥?

[英]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.

相关问题 我如何消除实时数据库中自动生成的密钥 firebase flutter - How do i eliminate the autogenerated key in realtime database firebase flutter Firebase 实时数据库:如何在 push().set() 之后获取唯一键? - Firebase Realtime Database: How to get the unique key after push().set()? Flutter-Firebase RTDB:从 Firebase 实时数据库读取数据是否需要推送 ID? - Flutter-Firebase RTDB : Is Push ID mandatory for reading data from Firebase Realtime database? Flutter 和 Firestore:如何首先获得最新的? - Flutter & Firestore: How do I get the most recent first? 如何在 flutter 中实时获取 firebase 数据库中孩子的价值? - how can i get the value of a child in firebase database realtime in flutter? 如何从 Firebase 实时数据库中获取数据到 Flutter 的列表中? - How to get data from Firebase Realtime Database into list in Flutter? 如何使用 flutter 从 firebase 实时数据库中检索特定数据? - How do I retrieve specific data from firebase realtime database using flutter? 如何使用 Flutter 仅从 firebase 实时数据库中的给定数字获取时间范围? - How can I get only the time range from the given figure in firebase realtime database using Flutter? 如何从 Flutter 中的 Firebase 实时数据库中按 categoryId 获取产品? - How can I get products by categoryId from Firebase Realtime Database in Flutter? 如何获取当前的 Firebase UID 并将其写入实时数据库? - How do I get the current Firebase UID and write it to Realtime Database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM