Erick Reyes
Erick Reyes

Reputation: 51

flutter get inserted id from Firebase

I'm doing an app in flutter (ecommerce) when there is no cart created or opened I must create an order and get the id to add products, but I can not get the id of the order created

Future<void> createOrder() async {  
    Order order = new Order('Guatemala', new DateTime.now().toString(), '${widget.userId}-0', 0, widget.userId);
    var orderRs =_database.reference().child("orders").push().set(order.toJson());
 }

_orderList = new List();
_orderQuery = _database
  .reference()
  .child("orders")
  .orderByChild("order_status")
  .equalTo('${widget.userId}-0')
  .limitToLast(1);

_onProductAddedSubscription = _productQuery.onChildAdded.listen(_onEntryAdded);
_onProductChangedSubscription = _productQuery.onChildChanged.listen(_onEntryChanged);
_onOrderAddedSubscription = _orderQuery.onChildAdded.listen(_onOrderAdded);
_onOrderChangedSubscription = _orderQuery.onChildChanged.listen(_onOrderChanged);

I need to be able to create the order and get the id to add the corresponding detail

Upvotes: 1

Views: 4130

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599571

You can get the order ID by hanging on to the DatabaseReference that is created when you all push():

var orderRef =_database.reference().child("orders").push()
orderRef.set(order.toJson());
print(orderRef.key);

Upvotes: 8

Related Questions