flutter从api获取数据并分配给数组

时间:2019-11-10 10:49:27

标签: arrays json http flutter

列出数据;

我想显示响应,但是当我尝试为数据分配jasonResponse时 我收到此错误

[ERROR:flutter / lib / ui / ui_dart_state.cc(148)]未处理的异常:类型“列表”不是“地图”类型的子类型

那我该怎么办

如何将对象数组分配给数据

Future<String> getJsonData() async{

    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    Map data = {'sale_id': sharedPreferences.getInt("token")};

    var jsonResponse;
    var response = await http.post(
      "http://",
      body: json.encode(data),
      headers: {
        "Content-Type": "application/json"

      },
    );

    if (response.statusCode == 200) {
      jsonResponse = json.decode(response.body);
      print('jsonRespnse');
      print(jsonResponse['developer_list']);
      if (jsonResponse != null) {
         setState(() {

          data = jsonResponse['developer_list'];

         });

1 个答案:

答案 0 :(得分:1)

您的data是类型Map,但是您正在分配类型List

只需添加具有您的值的键,如下所示:

data['developer_list'] = jsonResponse['developer_list'];

确保通过data['developer_list']访问该列表