颤振如何增加价值并展示

时间:2020-10-29 12:35:22

标签: flutter

我有这样的json数据

{
            "OrderID": 208,
            "CustomerID": 2,
            "TransactionNo": 80,
            "OrderNo": 1,
 
            "OrderDetails": [
                {
                
                    "ItemName": "Water",
                    "Price": 2.0,

                },
                {
  
                    "ItemName": "Water",
                    "Price": 2.0,
  
                }
            ],
}

我正在有状态窗口小部件中显示此数据。但问题是现在我需要计算价格并在小部件中显示。我需要对Orderdetails ['price']进行总结,并在“文本”窗口小部件中显示我现在被卡住了

  class OrderDetails extends StatefulWidget {
  Map<String, dynamic> text;

  // receive data from the FirstScreen as a parameter
  OrderDetails({Key key, @required this.text}) : super(key: key);

  @override
  _OrderDetailsState createState() => _OrderDetailsState();
}

class _OrderDetailsState extends State<OrderDetails> {
  @override
  Widget build(BuildContext context) {
    print(widget.text);

    double stackWidth = MediaQuery.of(context).size.width;
    double stackHeight = MediaQuery.of(context).size.height;
    return Scaffold(
      body: Column(
        children: <Widget>[
          Container(
            width: double.infinity,
            color: Colors.white,
            child: Padding(
              padding: const EdgeInsets.all(15.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  if (widget.text['CustomerOrders'] != null)
                    Text(
                      (widget.text['CustomerOrders'] as Map)['Name']
                              .toString() ??
                          "N/A",
                      style: TextStyle(
                          fontFamily: 'SFPROBOLD',
                          fontWeight: FontWeight.bold,
                          fontSize: 17),
                    ),

          Padding(
            padding: const EdgeInsets.all(15.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  'Total (incl, Tax)',
                  style: TextStyle(
                    fontSize: 15,
                  ),
                ),
                Text(
                  'I need to show the total value here',
                  style: TextStyle(
                      fontFamily: 'SFPROBOLD',
                      fontSize: 15,
                      fontWeight: FontWeight.bold),
                )
              ],
            ),
          ),


        ],
      ),
    ))]));
  }
}

您可以在代码中看到我正在显示widget.text之类的值,但不知道如何将OrderDetails的值存入并显示在文本控件中

2 个答案:

答案 0 :(得分:1)

var order = jsonDecode(your_json);
var details = order['OrderDetails'];
var total = 0.0;
details.forEach((i) {
  total+=i['Price'];
});

答案 1 :(得分:0)

要获取列表的总和,请使用此列表,假设您可以将价格作为列表获得。

int sum = price.fold(0, (previous, current) => previous + current);

我建议您使用类来解码json,而不要在地图上进行迭代。为了快速生成解码器类,请将您的json粘贴到https://quicktype.io/上,然后将其传递给FutureBuilder