获取类型';列表<;动态>';不是类型为';列表<&燃气轮机';JSON中的错误

获取类型';列表<;动态>';不是类型为';列表<&燃气轮机';JSON中的错误,json,dart,flutter,Json,Dart,Flutter,我正在解码一个响应体,我得到了错误: 'List<dynamic>' is not a subtype of type 'List<Example>' “列表”不是“列表”类型的子类型 我正在解析一个JSON对象的JSON数组,其中一个字段也是一个对象列表,我怀疑我的问题就源于此。我还使用json_序列化库。下面是我的代码,我省略了一些字段并更改了一些变量名,但它表示相同的代码: import 'package:json_annotation/json_annota

我正在解码一个响应体,我得到了错误:

'List<dynamic>' is not a subtype of type 'List<Example>' 
“列表”不是“列表”类型的子类型
我正在解析一个JSON对象的JSON数组,其中一个字段也是一个对象列表,我怀疑我的问题就源于此。我还使用json_序列化库。下面是我的代码,我省略了一些字段并更改了一些变量名,但它表示相同的代码:

import 'package:json_annotation/json_annotation.dart';

part 'example_model.g.dart';

@JsonSerializable()
class Example {

  (some fields here)
  final List<Random> some_urls;
  final List<String> file_urls;


  const Example({
    (some fields here)
    this.some_urls,
    this.file_urls,

  });

  factory  Example.fromJson(Map<String, dynamic> json) =>
      _$ ExampleFromJson(json);
}

@JsonSerializable()
class Random {
  final String field_1;
  final int field_2;
  final int field_3;
  final int field_4;
  final bool field_5;

  constRandom(
      {this.field_1, this.field_2, this.field_3, this.field_4, this.field_5});

  factory Random.fromJson(Map<String, dynamic> json) => _$RandomFromJson(json);
}
import'package:json_annotation/json_annotation.dart';
“示例_模型g.省道”部分;
@JsonSerializable()
课例{
(此处有一些字段)
最后列出一些URL;
最终列表文件\u URL;
常量示例({
(此处有一些字段)
这个,一些网址,,
此.u文件包含URL,
});
工厂示例.fromJson(映射json)=>
_$ExampleFromJson(json);
}
@JsonSerializable()
类随机{
最终字符串字段_1;
最终整型字段_2;
最终整型字段_3;
最终整型字段_4;
最终布尔场_5;
构造(
{this.field_1,this.field_2,this.field_3,this.field_4,this.field_5});
factory Random.fromJson(映射json)=>u$RandomFromJson(json);
}
从json_serializable生成的.g dart文件(编辑编码部分):

Example\u$ExampleFromJson(映射json){
返回示例(
一些URL:(json['some_URL']作为列表)
?地图((e)=>
e==null?null:Random.fromJson(e作为映射))
?.toList(),
文件URL:(json['file\u url']作为列表)?.map((e)=>e作为字符串)?.toList(),
}
Random$RandomFromJson(映射json){
返回随机数(
field_1:json['field_1']作为字符串,
field_2:json['field_2']作为int,
field_3:json['field_3']作为int,
field_4:json['field_4']作为int,
field_5:json['field_5']作为bool);
}
这是我未来的职能:

  Future<List<Example>> getData(int ID, String session) {
    String userID = ID.toString();
    var url = BASE_URL + ":8080/example?userid=${userID}";
    return http.get(url, headers: {
      "Cookie": "characters=${session}"
    }).then((http.Response response) {
      if (response.statusCode == 200) {
        var parsed = json.decode(response.body);
        List<Example> list = parsed.map((i) => Example.fromJson(i)).toList();
        return list;
      }
    }).catchError((e)=>print(e));
  }
Future-getData(int-ID,字符串会话){
字符串userID=ID.toString();
var url=BASE_url+“:8080/示例?userid=${userid}”;
返回http.get(url、标题:{
“Cookie”:“characters=${session}”
}).然后((http.Response){
如果(response.statusCode==200){
var parsed=json.decode(response.body);
List=parsed.map((i)=>Example.fromJson(i)).toList();
退货清单;
}
}).catchError((e)=>打印(e));
}

此代码创建一个
列表

您必须明确地将
List
强制转换为
List
,如下所示

List<Example> list = List<Example>.from(parsed.map((i) => Example.fromJson(i)));
List=List.from(parsed.map((i)=>Example.fromJson(i));
或者只是

var /* or final */ list = List<Example>.from(parsed.map((i) => Example.fromJson(i)));
var/*或final*/list=list.from(parsed.map((i)=>Example.fromJson(i));
另见


当我尝试Günter的解决方案时,我收到的
“MappedListIterable”不是“Iterable”类型的子类型

 var parsed = json.decode(response.body);
 var list = parsed.map((i) => Example.fromJson(i)).toList();
将解析后的数据转换到
列表
(而不是让它进入
动态
)为我解决了这个问题

 var parsed = json.decode(response.body) as List<dynamic>;
 var list = parsed.map((i) => Example.fromJson(i)).toList();
var parsed=json.decode(response.body)作为列表;
var list=parsed.map((i)=>Example.fromJson(i)).toList();

错误原因:

如果源
列表
的类型为
动态
对象
(比方说),并且直接将其分配给特定类型而不强制转换,则会出现此错误

List<dynamic> source = [1]; 
List<int> ints = source; // error
List source=[1];
List ints=source;//错误

解决方案:

您需要将您的
列表
转换为
列表
(所需类型),有很多方法可以实现。我在这里列出了一些方法:

  • List ints=List.from(源代码);
    
  • List ints=List.castFrom(源代码);
    
  • List ints=source.cast();
    
  • List ints=source.map((e)=>e as int.toList();
    
  • 您的代码:

    var parsed=json.decode(response.body);
    List=parsed.map((i)=>Example.fromJson(i)).toList();
    
    可替换为以下代码:

    import 'package:json_helpers/json_helpers.dart';
    
    
    final examples = response.body.jsonList((e) => Example.fromJson(e));
    

    而且一切都会按照您的预期运行…

    我得到错误类型“MappedListIterable”不是类型“Iterable”的子类型请尝试
    。从(…)
    而不是(…)
    。我仍在阅读并试图了解(
    和(
    从()
    is.been
    of
    希望输入列表已经具有正确的泛型类型,而使用
    from
    时,只有列表中的元素需要具有正确的类型。@maheshmnj您是根据哪个Dart版本铸造的?请参考此@maheshmnj您能分享特定thre的链接吗支持您最初陈述的广告--cast不再可用。请看一看,我想我可能误解了基于此答案评论的陈述
    List<dynamic> source = [1]; 
    List<int> ints = source; // error
    
    List<int> ints = List<int>.from(source);
    
    List<int> ints = List.castFrom<dynamic, int>(source);
    
    List<int> ints = source.cast<int>();
    
    List<int> ints = source.map((e) => e as int).toList();
    
    import 'package:json_helpers/json_helpers.dart';
    
    
    final examples = response.body.jsonList((e) => Example.fromJson(e));