【问题标题】:Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<List<dynamic>>'未处理的异常:类型“_InternalLinkedHashMap<String, dynamic>”不是“FutureOr<List<dynamic>>”类型的子类型
【发布时间】:2021-05-05 09:48:29
【问题描述】:

我正在尝试从服务器获取 json 数据

这里是代码:

 void main() async{
      List data = await getData();
      print(data);
      runApp(MyApp());
    }
    
    Future<List> getData() async {
      String myUrl = "https://dashboard.ssitanas.com/public/api/categories";
      http.Response response = await http.get(myUrl, headers: {
        'Accept': 'application/json',
      });
      return json.decode(response.body);
    
    }

有什么问题?

【问题讨论】:

标签: flutter dart


【解决方案1】:

从api传来的响应是一个Map,不是List,但是看东西,map里面好像有一个list

所以就这样做吧:

    var res = json.decode(response.body);
    var listData = res["data"]; 
    //assuming the list inside the map is called data
    return listData;

【讨论】:

    猜你喜欢
    • 2021-01-13
    • 2022-07-21
    • 2021-10-15
    • 1970-01-01
    • 2020-08-20
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多