flutter “_InternalLinkedHashMap〈String,dynamic>"不是类型”List“的子类型< dynamic>

e0bqpujr  于 2022-12-19  发布在  Flutter
关注(0)|答案(1)|浏览(131)

大家好,我需要你们的帮助,我想显示来自API的信息,但我收到一条错误消息,告诉我_InternalLinkedHashMap〈String,dynamic〉'不是类型'List '的子类型

Future<List<FactureModel>> getUser() async {
    var apiUrl = '/factures';

    http.Response response = await http.get(Uri.parse(_baseUrl + apiUrl));

    try {
      if (response.statusCode == 200) {
        List<dynamic> list = await json.decode(response.body);
        print(list);
        return list.map((e) => FactureModel.fromJson(e)).toList();
      } else {
        print("Something wrong");
        return <FactureModel>[];
      }
    } catch (e) {
      print(e);
      print("Api request failled");
      return <FactureModel>[];
    }

    //var info = rootBundle.loadString("json/data.json");
    //List<dynamic> list = json.decode(await info);
    //print(list);
    //return Future.delayed(
    //const Duration(seconds: 5), () => list.map((e) => e).toList());
  }
}
eivgtgni

eivgtgni1#

将解码后的响应直接存储到FractureModel而不是List中。2然后从FractureModel返回List即可。

相关问题