Flutter 如何在flatter的下拉列表中显示json嵌套数组?

Flutter 如何在flatter的下拉列表中显示json嵌套数组?,flutter,Flutter,我需要在下拉列表中显示行数据。感谢您的帮助 class _MyAppState extends State<MyAppdrop> { List<LeadService> payloadListdropdown; bool loading = false; String _mySelection; List<LeadService> payloadFromJson(String str) => List<LeadServ

我需要在下拉列表中显示行数据。感谢您的帮助

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,

  };
}


class\u MyAppState扩展状态{
地图付费列表下拉列表;
地图选举;
bool_加载=真;
requestMethodspinner()异步{
var url=“”;
映射头={
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”,
};
最终响应=等待http.post(url,头:头);
final responseJson=json.decode(response.body);
印刷品(responseJson);
打印(响应状态码);
如果(response.statusCode==200){
设置状态(){
加载=假;
payloadListdropdown=json.decode(response.body)[0];
_mySelection=payloadListdropdown['rows'][0];
});
}
否则{
设置状态(){
加载=假;
});
抛出异常(“加载internet失败”);
}
}
@凌驾
void initState(){
super.initState();
requestMethodspinner();
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:appBar(
标题:文本(“下拉列表”),
),
正文:新中心(
子项:\加载(&payloadListdropdown==null?
列表砖(
标题:文本('加载…'),
前导:CircularProgressIndicator(),
)
:
payloadListdropdown==null?
列表砖(
标题:文本('互联网不可用或找不到!'),
)
:下拉按钮(
项目:payloadListdropdown['rows'].map((项目){
返回新的DropdownMenuItem(
子项:新文本(项['sName']),
//其他领域,如sCode等。。
价值:项目,,
);
}).toList(),
更改后:(newVal){
设置状态(){
_mySelection=newVal;
打印(_mySelection.toString())
});
},
值:_mySelection,
),
),
);
}
}

我不明白这一点。你能帮我写上面的代码吗?这将是非常有帮助的。不,它不起作用。我得到了这个错误。错误:无法将参数类型“DropdownMenuItem函数(Map)”分配给参数类型“DropdownMenuItem函数(LeadService)”。子级:新建DropdownButton(项:payloadListdropdown.Map((项){print({payloadListdropdown[0]。行[1]。sName});返回新的DropdownMenuItem(子项:新文本(${payloadListdropdown[0]。行[0]。sName}”),);}).toList(),一旦更改:(newVal){setState((({u mySelection=newVal;});},值:{u mySelection,),通过使用上述代码,我只得到下拉列表中的第一个值。@斯堪的纳维亚请勿使用LeadService类/对象!您可以直接获取json并通过方法Map将其分配给Map。从(json_文件)将所有LeadService实例替换为Map实例
[
  {
    "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
  }
]
    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,
            ),
          ),
        );
      }
    }