How can I read and increment a number (in this case order number) from firebase using flutter?
Can this https://firebase.google.com/docs/database/flutter/read-and-write help you ?
For read : use final order = _ref.child('Orders/{$id}').get();
Stamp777
Updated on January 04, 2023Comments
-
Stamp777 over 1 year
I am using some of the following code to add an order to the Firebase Realtime Database using Flutter (Dart language) and this part is working.
DatabaseReference _ref = FirebaseDatabase.instance.ref(); final order = _ref.child('Orders'); order.push().set({ 'orderNum': orderNum, 'totalAmount': totalAmount, 'dateTime': timeStamp.toString(), 'cartProduct': cartProducts .map((cp) => { 'id': cp.id, 'title': cp.name, 'quantity': cp.quantity, 'price': cp.price }) .toList()
I am then reading and attempting to alter the order number and incrementing it by one using the following
String lastOrderNum = ""; int intOrderNum = 0; String newOrderNum = "2"; final DateTime dateee = DateTime.now(); DatabaseReference _readRef = FirebaseDatabase.instance.ref(); @override void initState() { super.initState(); _activateListeners(); } void _activateListeners() { final readOrder = _readRef.child('Orders/orderNum').onValue.listen((event) { final orderNum = event.snapshot.value; setState(() { lastOrderNum = orderNum.toString(); intOrderNum = int.parse(lastOrderNum) + 1; newOrderNum = intOrderNum.toString(); }); }); }
I think the issue is with my child path which is (Orders/orderNum) which is missing a step in the path which is where the auto generated unique ID goes which come from the use of ".push()"
order.push().set({
If I remove '.push()' and use my current path then the number auto increments but the previous order is overwritten.
The database entries look are as follows with order numbers being hardcoded:
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
How do I include the auto ID (example -N3ZdgEQIzsjLA5NCu3U) as part of my path , (Orders/???/orderNum)?
-
Stamp777 almost 2 yearsAdding {$id} to the path so it looks like ('Orders/{$id}/orderNum)' returns an error saying I need to change "id" to a name that is defined but I do not know what name is defined since I didn't define it. Is there another way to refer to this id?
-
mollusk almost 2 yearsI don't understand what you want to do. To modify a value of a command on any SQL server: You must retrieve the command you want (with the autoincremented id). Then modify the value of your ordernum.
DatabaseReference ref = FirebaseDatabase.instance.ref("Orders/N3ZdgEQIzsjLA5NCu3U"); await ref.set({"orderNum": 10,});
-
Stamp777 almost 2 yearsI am struggling to actually get the most recent autoincremented ID in order to do that. I am now trying to fetch key of the last order made but I still cannot get the key value (let say its "N3ZdgEQIzsjLA5NCu3U") that I can then use to get the order number.
-
mollusk almost 2 yearsFor last order you can use
Query lastchield = FirebaseDatabase.child("Orders") .orderByKey().limitToLast(1);