4

我对 JSON 数据进行了以下建模,其中每个订单中都有订单和商品。

数据模型

   class NewOrder {
      String _error;
      List<Content> _content;
    
      NewOrder({String error, List<Content> content}) {
        this._error = error;
        this._content = content;
      }
    
      String get error => _error;
      set error(String error) => _error = error;
      List<Content> get content => _content;
      set content(List<Content> content) => _content = content;
    
      NewOrder.fromJson(Map<String, dynamic> json) {
        _error = json['error'];
        if (json['content'] != null) {
          _content = new List<Content>();
          json['content'].forEach((v) {
            _content.add(new Content.fromJson(v));
          });
        }
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['error'] = this._error;
        if (this._content != null) {
          data['content'] = this._content.map((v) => v.toJson()).toList();
        }
        return data;
      }
    }
    
    class Content {
    
      String _orderNo;
      List<OrderItems> _orderItems;
    
      Content(
          {
          String orderNo,
          List<OrderItems> orderItems}) {
     
        this._orderNo = orderNo;
        this._orderItems = orderItems;
      }
    
      set orderNo(String orderNo) => _orderNo = orderNo;
      List<OrderItems> get orderItems => _orderItems;
      set orderItems(List<OrderItems> orderItems) => _orderItems = orderItems;
    
      Content.fromJson(Map<String, dynamic> json) {
      
        _orderNo = json['order_no'];
        if (json['order_items'] != null) {
          _orderItems = new List<OrderItems>();
          json['order_items'].forEach((v) {
            _orderItems.add(new OrderItems.fromJson(v));
          });
        }
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
     
        data['order_no'] = this._orderNo;
        if (this._orderItems != null) {
          data['order_items'] = this._orderItems.map((v) => v.toJson()).toList();
        }
        return data;
      }
    }
    
    class OrderItems {
      String _compCode;
    
      OrderItems({String compCode, String compName, String orderNo}) {
        this._compCode = compCode;
      }
    
      String get compCode => _compCode;
      set compCode(String compCode) => _compCode = compCode;
    
      OrderItems.fromJson(Map<String, dynamic> json) {
        _compCode = json['comp_code'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['comp_code'] = this._compCode;
    
        return data;
      }
    }

JSON数据

{
    "error": "false",
    "content": [
        {
            "order_no": "16",
            "order_items": [
                {
                    "comp_code": "4",
                },
                {
                    "comp_code": “5”,
                }
            ]
        },
        {
            "order_no": "18",
            "order_items": [
                {
                    "comp_code": “9”,
                },
                {
                    "comp_code": “11”,
                },
                {
                    "comp_code": “7”,
                },
               
            ]
        }
    ]
}

我需要在 PageView.builder(Horizo​​ntal Scroll) 和嵌套的 ListView.builder(Vertical Scroll) 中显示数据,这个模型我已经创建了。

我使用以下代码将 JSON 日期与上述配置的模型相关联,并且此功能成功运行

Future<NewOrder> getdetailsoforders(String userid, String companycode) async {

    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    Map data = {
      'user_id': userid,
      'company_code':companycode
    };


    var response = await http.post(newapi, body: data);
    if(response.statusCode == 200) {
      
      jsonResponse = json.decode(response.body);

    return NewOrder.fromJson(jsonResponse);


    }
    

}

现在我需要将 JSON 数据字段关联到我使用以下小部件的小部件模型

    FutureBuilder(
            future: _future,
            builder: (context, AsyncSnapshot<Payload> snapshot) {
              switch (snapshot.connectionState) {
                case ConnectionState.none:
                  return Text('none');
                case ConnectionState.waiting:
                  return Center(child: CircularProgressIndicator());
                case ConnectionState.active:
                  return Text('');
                case ConnectionState.done:
                  if (snapshot.hasError) {
                    return Text(
                      '${snapshot.error}',
                      style: TextStyle(color: Colors.red),
                    );
                  } else {
   return PageView.builder(
                        scrollDirection: Axis.horizontal,
                        itemCount: snapshot.data.content.length,// length of total orders

                        itemBuilder: (context, index) {
                          return Column(
                                          children:<Widget>[
                                    Text('Order Number to be Displayed here'),
                                    ListView.builder(
                                  
                                  shrinkWrap: true,
                                  itemCount: //lenght of the items in the order to be determined,
                                  itemBuilder: (context, index) {
                                   return Column(
                                      children: [
                                         Text('Item Name'),
                                         Text('Item description here')

                                      ],
                                    );
                                  },
                                ),



                                        ])


                          
                        });
                  }
              }
            })

对于订单的最终清单及其各自的项目,我需要从模型中获取字段,但我无法这样做,请指导我如何获取这些详细信息。我需要Order NO.来自内容模型的 ,然后在同一个模型中,我需要获取 的长度,order_items然后是这些订单项的字段值。

我是学习 JSON Flutter 的新手,这就是我面临这个问题的原因,请指导我解决方案。

我知道我写了很多代码,但它是为了提供一个清晰的画面,究竟需要做什么。

4

1 回答 1

0

代替

builder: (context, AsyncSnapshot<Payload> snapshot)

builder: (context, snapshot)

访问数据如:

Text(snapshot.data.content[index].orderNo)
于 2020-08-30T06:04:13.183 回答