How to pass data from one table to another table using flutter (get and post on 2 different tables)?

Viewed 19

I am creating an ecommerce Android flutter application, and I am new to this dart language. I need to get data from one table and post it to another table, where the API is built in .NET Core using a SQL Server database.

This is my code:

httpService.getPosts().then((value) {
                        if (value != null) {
                          value.forEach((element) {
                            httpServices.addPosts(
                              0,
                              element.cartProductID, element.productBrandId,
                              element.cartUserID, element.item,
                              element.quantity, element.price,
                              element.totalPrice,
                              element.discount,
                              // element.isOrdered,
                              element.paymentID,
                              element.paymentMode,
                              element.date,
                            );
                          });

My get method

class GetOrderHttpService with ChangeNotifier {
  Future<List<OrderTotal>> getPosts() async {
    Response res =
        await http.get(Uri.https('********'));

    if (res.statusCode == 200) {
      List<dynamic> body = jsonDecode(res.body);
      List<OrderTotal> posts = body
          .map(
            (dynamic dynamic) => OrderTotal.fromJson(dynamic),
          )
          .toList();
      notifyListeners();
      return posts;
    } else {
      throw "Unable to retrieve posts.";
    }
  }
}

Future<bool> addPosts(
    int orderID,
    int orderProductID,
    int productBrandId,
    int orderUserID,
    String item,
    int quantity,
    double price,
    double totalPrice,
    double discount,
    int paymentID,
    String? paymentMode,
    DateTime date,
  ) async {
    var response = await http.post(
        Uri.https('************'),
        body: jsonEncode({
          'orderID': orderID,
          'orderProductID': orderProductID,
          'productBrandId': productBrandId,
          'orderUserID': orderUserID,
          'item': item,
          'quantity': quantity,
          'price': price,
          'totalPrice': totalPrice,
          'discount': discount,
          'paymentID': paymentID,
          'paymentMode': paymentMode,
          'date': date
        }),
        headers: {
          "Accept": "application/json",
          "content-type": "application/json"
        });
    var data = response.body;
    if (response.statusCode == 200) {
      return true;
    } else
      throw Exception();
  }
}

It successfully retrieves the data and passes it on to the future post method, but the database is not updated. When the breakpoint hits the post method, it doesn't go through the code and doesn't get any status code. Thank you

1 Answers

Notice you are using Future in both your get() and post() methods, but, when calling these methods you are not using the "await" keyword. You should use it every time you call a Future function assuring you are waiting that method to complete and retrieve data successfully. It might work without it (as you say your get method works) but, in more complex situations this might not be the case due to asynchronous nature of these type of functions.

Your code should look like this:

await httpService.getPosts().then((value) async {
                    if (value != null) {
                      value.forEach((element) {
                        await httpServices.addPosts(
                          0,
                          element.cartProductID, element.productBrandId,
                          element.cartUserID, element.item,
                          element.quantity, element.price,
                          element.totalPrice,
                          element.discount,
                          // element.isOrdered,
                          element.paymentID,
                          element.paymentMode,
                          element.date,
                        );
                      });

Hope this works. Have a nice day!

Related