【问题标题】:Getting type 'List<dynamic>' is not a subtype of type 'List<...>' error in JSON获取类型 'List<dynamic>' 不是 JSON 中类型 'List<...>' 错误的子类型
【发布时间】:2019-03-24 00:14:51
【问题描述】:

我正在解码响应正文并收到错误消息:

'List<dynamic>' is not a subtype of type 'List<Example>' 

我正在解析 JSON 对象的 JSON 数组,其中一个字段也是对象列表,我怀疑我的问题源于此。我也在使用 json_serializable 库。下面是我的代码,我省略了一些字段并更改了一些变量名,但它代表相同的代码:

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);
}

来自 json_serializable 制作的 .g dart 文件(省略了编码部分):

Example _$ExampleFromJson(Map<String, dynamic> json) {
  return Example(

      some_urls: (json['some_urls'] as List)
          ?.map((e) =>
      e == null ? null : Random.fromJson(e as Map<String, dynamic>))
          ?.toList(),
      file_urls: (json['file_urls'] as List)?.map((e) => e as String)?.toList(),

}

Random _$RandomFromJson(Map<String, dynamic> json) {
  return Random(
      field_1: json['field_1'] as String,
      field_2: json['field_2'] as int,
      field_3: json['field_3'] as int,
      field_4: json['field_4'] as int,
      field_5: json['field_5'] as 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));
  }

【问题讨论】:

    标签: json dart flutter


    【解决方案1】:

    这段代码创建了一个List&lt;dynamic&gt;

    parsed.map((i) => Example.fromJson(i)).toList();
    

    您必须像这样将List&lt;dynamic&gt; 显式转换为List&lt;Example&gt;

    List<Example> list = List<Example>.from(parsed.map((i) => Example.fromJson(i)));
    

    或者只是

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

    另见

    【讨论】:

    • 我得到错误类型 'MappedListIterable' is not a subtype of type 'Iterable'
    • 尝试.from(...) 而不是.of(...)。我仍在阅读并尝试了解of()from() 之间的确切区别。
    • 似乎of 期望输入列表已经具有正确的泛型类型,而对于from,只有列表中的元素需要是正确的类型。
    • 很棒的答案。你拯救了我的一天。 @GünterZöchbauer
    【解决方案2】:

    您的代码:

    var parsed = json.decode(response.body);
    List<Example> list = parsed.map((i) => Example.fromJson(i)).toList();
    

    可以用这个代码代替:

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

    一切都会如你所愿......

    【讨论】:

      【解决方案3】:

      错误原因:

      当您的源 List 的类型为 dynamicObject(比方说)并且您直接将其分配给特定类型而不进行强制转换时,您会收到此错误。

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

      解决方案:

      您需要将List&lt;dynamic&gt; 转换为List&lt;int&gt;(所需类型),有很多方法可以做到这一点。我在这里列出一些:

      1. List<int> ints = List<int>.from(source);
        
      2. List<int> ints = List.castFrom<dynamic, int>(source);
        
      3. List<int> ints = source.cast<int>();
        
      4. List<int> ints = source.map((e) => e as int).toList();
        

      【讨论】:

      【解决方案4】:

      当我尝试 Günter 的解决方案时,我收到了 'MappedListIterable&lt;dynamic, dynamic&gt;' is not a subtype of type 'Iterable&lt;Example&gt;

       var parsed = json.decode(response.body);
       var list = parsed.map((i) => Example.fromJson(i)).toList();
      

      将解析后的数据转换为List&lt;dynamic&gt;(而不是仅仅让它转到dynamic)为我解决了这个问题。

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

      【讨论】:

        猜你喜欢
        • 2021-09-21
        • 2021-11-10
        • 1970-01-01
        • 2022-12-02
        • 2021-08-13
        • 2020-06-27
        • 2021-12-29
        • 2023-01-08
        相关资源
        最近更新 更多