change order Status that is on Order ID or User ID in dart

Viewed 33

Can someone tell me how can I change order Status based on Order ID which we get after order is placed. Tried getting the document id and based on order ID but not working with where query need to change its status from the dropdown. Please help how to do it properly. Here is the code for dropdown widget and fetchorder function that gets it values from firebase.

class OrderListScreen extends StatefulWidget {
  const OrderListScreen({Key? key}) : super(key: key);

  @override
  State<OrderListScreen> createState() => _OrderListScreenState();
}

class _OrderListScreenState extends State<OrderListScreen> {
  String? newvalue;

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Container(
        child: Column(
          children: <Widget>[
            Text(" Please select the order status from the dropdown Below:",
                style: TextStyle(backgroundColor: Colors.orange)),
            Container(
                child: Material(
              child: DropdownButton<String>(
                value: newvalue,
                items: <String>[
                  'Pending',
                  'Confirmed',
                  'Dispatched',
                  'Received'
                ].map((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
                onChanged: (String? newvalue) {
                  value:
                  newvalue;
                  var snapshots = FirebaseFirestore.instance
                      .collection('orders').doc()
                     
                      .update({"orderStatus": newvalue});
                  setState(() async {
                    this.newvalue = newvalue;
                    
                  });
                },
              ),
            )),
          ],
        ),
      ),
    );
  }
}

Function that fetch order/user id

 class AdminOrderController extends GetxController {
        fetchAllOrders() async {
    
        List<Order> orders = [];
        await firestoreInstance
            .collection("orders")
            .get()
            .then((value) => value.docs.forEach((element) {
                  if (true) {
                    Timestamp timestamp = element.data()['orderDateTime'];
                    Order order = Order(
                      bookCounts: element.data()['bookCounts'],
                      bookIDsList: element.data()['bookIDsList'],
                      orderDateTime: timestamp.toDate(),
                      orderStatus: element.data()['orderStatus'],
                      totalPrice: element.data()['totalPrice'],
                      transactionImageLink: element.data()['transactionImageLink'],
                      userID: element.data()['userID'],
                      orderID: element.data()['orderID'],
                      deliveryAddress: element.data()['deliveryAddress'] ??
                          Address(
                              name: "name",
                              phone: "phone",
                              userID: "userID",
                              address: "address",
                              city: "city",
                              state: "state",
                              zip:
                                  "zip"), 
                    );
                    orders.add(order);
                  }
                }));
        return orders;
      }
    }
1 Answers

In the query, you need to pass the Order Id in Doc. Because you are getting the collection, not a specific node. First, collect collection, document, and then update the OrderStatus value.

var collection = FirebaseFirestore.instance.collection('orders');
collection 
    .doc('Pass order id here') // <-- Doc ID where data should be updated.
    ..update({"orderStatus": newvalue}) // <-- Updated data
    .then((_) => print('order status is Updated'))
    .catchError((error) => print('Update failed: $error'));
Related