需要将JSON数据获取字段配置为flutter中的小部件元素

问题描述

我具有以下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>();
     
        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>();
        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(水平滚动)和嵌套的ListView.builder(垂直滚动)中的数据。

我使用下面的代码将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,这就是为什么我要面对这个问题,请向我提供解决方案。

我知道我已经写了很多代码,但这是为了提供清晰的图片,究竟需要做什么。

解决方法

替换

eax=esi+edi

使用

builder: (context,AsyncSnapshot<Payload> snapshot)

访问数据,例如:

builder: (context,snapshot)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...