如何在Flutter中的DropDown中显示JSON嵌套数组?

马拉里

我需要在下拉列表中显示行数据。任何帮助表示赞赏。

class _MyAppState extends State<MyAppdrop> {
  List<LeadService> payloadListdropdown;
  bool loading = false;
  String _mySelection;

  List<LeadService> payloadFromJson(String str) =>
      List<LeadService>.from(
          json.decode(str).map((x) => LeadService.fromJson(x)));
  String payloadToJson(List<LeadService> data) =>
      json.encode(List<dynamic>.from(data.map((x) => x.toJson())));


  Future<List<LeadService>> requestMethodspinner() async {
    var url = "";
    Map<String, String> headers = {
      'Content-type': 'application/json',
      'Accept': 'application/json',
    };
    final response = await http.post(url, headers: headers);
    final responseJson = json.decode(response.body);
    print(responseJson);
    print(response.statusCode);
    setState(() {
      loading = true;
      payloadListdropdown = payloadFromJson(response.body);
      loading = false;
    });
    if (response.statusCode == 200) {}
    else {
      throw Exception('Failed to load internet');
    }
    return payloadListdropdown;
  }


  @override
  void initState() {
    super.initState();
    this.requestMethodspinner();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("DropDown"),
      ),
      body: new Center(
        child: new DropdownButton(
          items: payloadListdropdown.map((item) {
            return new DropdownMenuItem(
              child: new Text("${payloadListdropdown[0].rows[1].sName}"),
              value: item[1].toString(),
            );
          }).toList(),
          onChanged: (newVal) {
            setState(() {
              _mySelection = newVal;
            });
          },
          value: _mySelection,
        ),
      ),
    );
  }
}
class LeadService {
  int status;
  String msg;
  List<Rows> rows;
  String sql;
  dynamic reserved;
  LeadService({
    this.status,
    this.msg,
    this.rows,
    this.sql,
    this.reserved,
  });
  factory LeadService.fromJson(Map<String, dynamic> json) => LeadService(
    status: json["status"],
    msg: json["msg"],
    rows: List<Rows>.from(json["rows"].map((x) => Rows.fromJson(x))),
    sql: json["sql"],
    reserved: json["reserved"],
  );
  Map<String, dynamic> toJson() => {
    "status": status,
    "msg": msg,
    "rows": List<dynamic>.from(rows.map((x) => x.toJson())),
    "sql": sql,
    "reserved": reserved,
  };
}
class Rows {

  String ServicesId;
  String sName;
  String sCode;

  Rows({
    this.ServicesId,
    this.sName,
    this.sCode,

  });
  factory Rows.fromJson(Map<String, dynamic> json) => Rows(
    //dropdownservicelist
    ServicesId:json["ServicesId"],
    sName:json["sName"],
    sCode:json["sCode"],

  );

  Map<String, dynamic> toJson() => {
    //dropdownservicelist
    "ServicesId" :ServicesId,
    "sName" : sName,
    "sCode" : sCode,

  };
}


Json数据:

[
  {
    "status": 200,
    "msg": "Ok",
    "refcode": -1,
    "trows": 13,
    "rows": [
      {
        "ServicesId": "3",
        "sName": "CFS",
        "sCode": "CF"
      },
      {
        "ServicesId": "2",
        "sName": "Container aGENT",
        "sCode": "CA"
      },
      {
        "ServicesId": "1",
        "sName": "Custom",
        "sCode": "CB"
      }
    ],
    "sql": "Services",
    "reserved": null
  }
]
Naveen Avidi
    class _MyAppState extends State<MyAppdrop> {
      Map payloadListdropdown;
      Map _mySelection;
      bool _loading = true;

      requestMethodspinner() async {
        var url = "";
        Map<String, String> headers = {
          'Content-type': 'application/json',
          'Accept': 'application/json',
        };
        final response = await http.post(url, headers: headers);
        final responseJson = json.decode(response.body);
        print(responseJson);
        print(response.statusCode);

        if (response.statusCode == 200) {
          setState(() {
          loading = false;
          payloadListdropdown = json.decode(response.body)[0];
          _mySelection = payloadListdropdown['rows'][0];
        });
       }
        else {
          setState((){
          loading = false;
          });
          throw Exception('Failed to load internet');
        }
      }


      @override
      void initState() {
        super.initState();
        requestMethodspinner();
      }

      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: AppBar(
            title: Text("DropDown"),
          ),
          body: new Center(
            child: _loading && payloadListdropdown == null ?
              ListTile(
                 title: Text('Loading...'),
                 leading:CircularProgressIndicator(),
              )
              :
              payloadListdropdown == null ?
              ListTile(
                 title: Text('Internet not available or Nothing found !'),
              )
              : DropdownButton(
              items: payloadListdropdown['rows'].map<DropDownMenuItem<Map>>((item) {
                return new DropdownMenuItem(
                  child: new Text(item['sName']),
//Other fields like sCode etc..
                  value: item,
                );
              }).toList(),
              onChanged: (newVal) {
                setState(() {
                  _mySelection = newVal;
                  print(_mySelection.toString())
                });
              },
              value: _mySelection,
            ),
          ),
        );
      }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Golang中的嵌套JSON对象中解析嵌套数组?

Mgo如何在嵌套数组中查找嵌套文档?

如何在猫鼬中查询嵌套数组

如何在angular2的表单数组中显示嵌套数组数据?

如何在postgres json列中查询嵌套数组?

如何在JavaScript中的嵌套数组中查找值

如何在Vue搜索组件中显示嵌套数组-Laravel

在Powershell中显示Json嵌套数组

如何在ReactJS中以JSON显示嵌套数组数据?

如何在Swagger UI中显示body参数的嵌套数组示例?

如何在vue.js组件中显示嵌套数组数据

如何从flutter应用程序在Cloud Firestore中记录带有嵌套数组的json

如何在嵌套数组中过滤JSON对象数组

如何在Excel VBA-JSON中从JSON嵌套数组获取数据

如何在MSSQL中更新JSON中的嵌套数组

如何在API原生的react中显示嵌套数组数据?

如何在Vue中循环嵌套数组并每5秒显示数据?

MongoDB:如何在嵌套数组中设置字段值

如何在MongoDB中更新嵌套数组中的数据

如何在Codeigniter中显示嵌套数组(国家-省-城市层次结构)

如何在模板中显示嵌套数组

AngularJS-如何在ng-repeat中单击父数组时显示嵌套数组

如何在嵌套数组中实现多字段更新

如何在angular api调用中显示嵌套数组对象

如何在 Flutter 中解析嵌套的 JSON 数组?

如何在 React-Native 的屏幕上访问嵌套数组 JSON 数据中的嵌套对象

MongoDB:如何在嵌套数组中插入对象?

如何在双嵌套数组中添加或删除元素?

如何在 ngx 数据表中显示嵌套数组对象?