gpt4 book ai didi

android - Flutter async/await 在 forEach 中不起作用

转载 作者:行者123 更新时间:2023-12-03 03:51:00 31 4
gpt4 key购买 nike

我不知道这里缺少什么。这是一个应该向客户下订单的功能。整个逻辑非常简单:

如果购物车中有 1 个商品,则仅执行一次。如果有更多,则将订单附加到第一个订单号。但由于某种原因,它不会等到 orderRepo.addOrder 执行最初完成。

  dynamic firstOrderNumber = 0;
var existingOrder = false;

void addOrder(List<Cart> carts) async { --> Coming to add the list of orders
carts.forEach((_cart) async { --> Going to add each items in cart

x += 1;
print("\nVALUE of x inside addOrder = $x"); -> Adding X flag here to check
Order _order = new Order();

_order.productOrders = new List<ProductOrder>();
_order.tax = _cart.product.store.defaultTax;
_order.deliveryFee = payment.method == 'Pay on Pickup'
? 0
: _cart.product.store.deliveryFee;
OrderStatus _orderStatus = new OrderStatus();
_orderStatus.id = '1';
_order.orderStatus = _orderStatus;
_order.deliveryAddress = payment.method == 'Pay on Pickup'
? new Address()
: settingRepo.deliveryAddress.value;
_order.hint = ' ';
ProductOrder _productOrder = new ProductOrder();
_productOrder.quantity = _cart.quantity;
_productOrder.price = _cart.product.price;
_productOrder.id = _cart.product.id;
_productOrder.product = _cart.product;
_productOrder.options = _cart.options;

_order.productOrders.add(_productOrder);
print("Current value of EXISTING : $existingOrder");
if (!existingOrder) {
print("\n\nEntering not existing order....\n");
await orderRepo.addOrder(_order, this.payment).then((value) { ---> Doesn't wait. PROBLEM
if (value is Order) {
print("The value is now $value");
firstOrderNumber =
value.id.toString(); //catching order number to append
print("FIRSTORDERNUMBER : $firstOrderNumber");
setState(() {
loading = false;
});
}
});
print("Settting value of existing order now...");
existingOrder = true;
print("Existing order is now $existingOrder");
}
// print("Existing order is now $existingOrder");
else {
print("\n\nEntering existing order....\n");
orderRepo
.addAdditionalOrder(
_productOrder, _cart, _order, firstOrderNumber, this.payment)
.then((value) {
if (value is Order) {
setState(() {
loading = false;
});
}
});
}
});
}

控制台输出:

I/flutter ( 6925): VALUE of x inside addOrder = 1
I/flutter ( 6925): Current value of EXISTING : false
I/flutter ( 6925): Entering not existing order....
I/flutter ( 6925): VALUE of x inside addOrder = 2 ---> See the problem. It came back to increment X before the await is complete.
I/flutter ( 6925): Current value of EXISTING : false
I/flutter ( 6925): Entering not existing order....

//现在它给出了之前应该等待的AWAIT结果......

I/flutter ( 6925): The response of first order is : <JSON RESPONSE from first addOrder execution...coming late..>
I/flutter ( 6925): The value is now Instance of 'Order'
I/flutter ( 6925): FIRSTORDERNUMBER : 141
I/flutter ( 6925): Settting value of existing order now...
I/flutter ( 6925): Existing order is now true
I/flutter ( 6925): The response of first order is :

有什么提示吗?我怎么能不执行任何其他事情,直到 await orderRepo.addOrder(_order, this. payment).then((value) { 完成???已经尝试了好几天的错误

最佳答案

尝试ma​​p()像这样


await Future.wait(carts.map((_cart)async{

//your code here


}));

关于android - Flutter async/await 在 forEach 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63466257/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com