在JSON中获取类型'List <dynamic>'不是类型'List <...>'的子类型

Dan*_*T29 4 json dart flutter

我正在解码响应正文,但出现错误:

'List<dynamic>' is not a subtype of type 'List<Example>' 
Run Code Online (Sandbox Code Playgroud)

我正在解析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);
}
Run Code Online (Sandbox Code Playgroud)

从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);
}
Run Code Online (Sandbox Code Playgroud)

这是我将来的功能:

  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));
  }
Run Code Online (Sandbox Code Playgroud)

Gün*_*uer 17

此代码创建了一个 List<dynamic>

parsed.map((i) => Example.fromJson(i)).toList();
Run Code Online (Sandbox Code Playgroud)

用代替

List<Example> list = List<Example>.from(parsed.map((i) => Example.fromJson(i)));
Run Code Online (Sandbox Code Playgroud)

要不就

var /* or final */ list = List<Example>.fromn(parsed.map((i) => Example.fromJson(i)));
Run Code Online (Sandbox Code Playgroud)

也可以看看

  • 似乎of期望输入列表已经具有正确的泛型类型,而对于from而言,仅列表中的元素需要具有正确的类型。 (2认同)

Cop*_*oad 9

错误原因:

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

List<dynamic> source = [1]; 
List<int> ints = source; // error
Run Code Online (Sandbox Code Playgroud)

解决方案:

您需要强制转换List<dynamic>List<int>(所需类型),有很多方法可以做到。我在这里列出一些:

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