【问题标题】:Not able to use the updated value -- Flutter无法使用更新后的值——Flutter
【发布时间】:2019-05-13 10:46:45
【问题描述】:

我正在更新 else if 中的 totalPayable 值,但是当我调用 getOrder() 时,我无法在代码中的任何地方使用更新后的值。是 API 问题还是有人可以帮我处理代码?

else if (isOrderInitiated == false){
    getCleintOrderFromApi();
    debugPrint("ifelse" + totalPayable.toString());
}

getClientOrderFromApi() {
    orders.clear();
    totalPayable = 0.0;
    api.getCleintOrder().then((list) {
        list.forEach((order) {
            if(order.is_placed) {
                order.status = ORDER_STATUS[0];
            } else if(!order.is_placed) {
                 order.status = ORDER_STATUS[2];
             }
            debugPrint("ORDER_STATUS from remote" + order.status);
            for (var j = 0; j < order.items.length; j++) {
                FoodItemOrder item = order.items[j];
                totalPayable = totalPayable + item.unitPrice * item.quantity;
                debugPrint(" total payable in order "+ totalPayable.toString());
            }
            orders.add(order);
            debugPrint("itemsin order"+orders.last.items.length.toString());
            });
        currentOrderList.clear();
        currentOrderList.addAll(orders);
        orderItemsSink.add(orders);
        });
    } 
} 

getClientOrderFromApi() 中的debugPrint 显示更新后的结果,但如果debugPrint(" ifelse "+ totalPayable.toString()); 没有显示更新后的值,这就是为什么我使用totalPayable 时没有显示所需的值。

【问题讨论】:

    标签: dart flutter


    【解决方案1】:
        Future<List<Order>> getCleintOrder() async {
            MakeOrder clientOrderRequest = MakeOrder(); //currentCafe;
            clientOrderRequest.caffeID = currentCafe.caffeId;
            clientOrderRequest.tableidtimestamp = currentUser.tableIdTimestamp;
            // offset = 160;
        String url = BASE_URL + BASE_URL_GET_CLIENT_ORDER;
        debugPrint("requesting getCleintOrder --- \n" +
            url +
            "\n " +
            clientOrderRequest.toClietnOrderJson().toString());
        var response = await http
            .post(
          url,
          headers: {
            HttpHeaders.contentTypeHeader: 'application/json',
            // HttpHeaders.authorizationHeader : ''
          },
          body: json.encode(clientOrderRequest.toClietnOrderJson()),
        )
            .catchError(
          (error) {
            ////debugPrint(error.toString());
            return false;
          },
        );
    
        var jsonObj = json.decode(response.body);
        ////debugPrint(jsonObj.toString());
        List<Order> orders = [];
    
        try {
          jsonObj.forEach((newJson) {
    
            List<Order> orderList = (newJson["orders"] as List)
                .map((neworderJson) => Order.fromJSON(neworderJson))
                .toList().cast<Order>();
            orders.addAll(orderList);
    
          });
        } catch (e) {
          ////debugPrint(e.toString());
        }
    
        // debugPrint("total client orders  " + orders.length.toString());
    
        return orders;
      }
    

    这是我的 api 调用以供参考。

    【讨论】:

      猜你喜欢
      • 2021-04-02
      • 2020-08-29
      • 2017-09-17
      • 2022-12-01
      • 1970-01-01
      • 2020-10-24
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多