_ TypeError(类型'List'不是类型'Map'的子类型)flutter

问题描述 投票:0回答:2

我在这里做了类似的帖子:problem,但我做了同样的事情,但我总是遇到问题...

我有我的课程:

class PollQuestionsDto {
  int id;
  String entitled;
  int difficulty;
  List<Answer> answers;

  PollQuestionsDto({this.id, this.entitled, this.difficulty, this.answers});

  PollQuestionsDto.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    entitled = json['entitled'];
    difficulty = json['difficulty'];
    if (json['answers'] != null) {
      answers = new List<Answer>();
      json['answers'].forEach((v) {
        answers.add(new Answer.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['entitled'] = this.entitled;
    data['difficulty'] = this.difficulty;
    if (this.answers != null) {
      data['answers'] = this.answers.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

询问API的方法:

Future<List<PollQuestionsDto>> getPollQuestions() async {
    String url = 'http://10.0.2.2:3000/v1/api/poll/40/question?page=0';
    //String url = 'http://10.0.2.2:3000/v1/api/poll/$idPoll/question?page=0';
    String token = await Candidate().getToken();
    final response = await http.get(url, headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Type': 'application/json; charset=utf-8',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token',
    });
    if (response.statusCode == 200) {
      print(jsonDecode(response.body));
      /*List pollsList = jsonDecode(response.body) ;
      List<PollQuestionsDto> polls = [];
      for (var themeMap in pollsList) {
        polls.add(PollQuestionsDto.fromJson(themeMap));
      }
      print(polls);
      print('Get Poll ${response.body}');*/
    } else {
      throw Exception('Failed get poll questions');
    }
  }

当我询问我的API时,请求的结果是:

{结果:[{id:2,标题:Le langageutilisésous Flutter est le dart?,难度:1,答案:[{id:9,标题:Oui,isCorrect:true},{id:10,标题:非,isCorrect:false}]}],pageCount:3,下一个:http://localhost:3000/v1/api/poll/40/question?page=1,上一个:}

当我想在列表中显示结果时,我有相同的错误:

_ TypeError(类型“列表”不是类型“地图”的子类型)

为什么?

编辑:

Future<List<PollQuestionsDto>> getPollQuestions() async {
    String url = 'http://10.0.2.2:3000/v1/api/poll/40/question?page=0';
    //String url = 'http://10.0.2.2:3000/v1/api/poll/$idPoll/question?page=0';
    String token = await Candidate().getToken();
    final response = await http.get(url, headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Type': 'application/json; charset=utf-8',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token',
    });
    if (response.statusCode == 200) {
      List pollsList = jsonDecode(response.body) ;
      List<PollQuestionsDto> polls = [];
      for (var themeMap in pollsList) {
        polls.add(PollQuestionsDto.fromJson(themeMap));
      }
      print(polls);
      print('Get Poll ${response.body}');
    } else {
      throw Exception('Failed get poll questions');
    }
  }
  PollQuestionsDto.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    entitled = json['entitled'];
    difficulty = json['difficulty'];
    if (json['answers'] != null) {
      answers = new List<Answer>();
      json['answers'].forEach((v) {
        answers.add(new Answer.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['entitled'] = this.entitled;
    data['difficulty'] = this.difficulty;
    if (data['answers'] != null) {
      data['answers'] = this.answers.map((v) => v.toJson()).toList();
    }
    return data;
  }

  @override String toString() => toJson().toString();

  static List<PollQuestionsDto> arrayOfQuestionsFromJson(String json) {
    return (jsonDecode(json) as List).map((e) => PollQuestionsDto.fromJson(e)).toList();
  }

mobile flutter dart flutter-layout
2个回答
0
投票

您需要将json['answers']强制转换为List,例如>

    if (json['answers'] != null) {
      answers = (json['answers'] as List).map((v) => Answer.fromJson(v)).toList();
    }

0
投票

如果结果为

© www.soinside.com 2019 - 2024. All rights reserved.