【发布时间】:2020-02-01 18:35:24
【问题描述】:
我在这里做了一个类似的帖子: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 langage utilisé sous Flutter est le dart?,难度:1,答案:[{id:9,标题:Oui,isCorrect:true},{id:10,标题:Non,isCorrect:false}]}],pageCount:3,下一个:http://localhost:3000/v1/api/poll/40/question?page=1,上一个:}
当我想在列表中生成结果时,我有同样的错误:
_TypeError(类型'List'不是类型'Map'的子类型)
为什么?
【问题讨论】:
标签: mobile flutter dart flutter-layout