我在这里做了类似的帖子: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;
}
}
ask 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,标题:Non,isCorrect:false}]}],pageCount:3,下一个:http://localhost:3000/v1/api/poll/40/question?page=1,上一个:}
当我想在列表中显示结果时,我也遇到相同的错误:
_TypeError(“列表”类型不是“地图”类型的子类型)
为什么?
答案 0 :(得分:1)
如果结果为
{
"results": [
{
"id": 2,
"entitled": "Le langage utilisé sous Flutter est le dart ?",
"difficulty": 1,
"answers": [
{
"id": 9,
"entitled": "Oui",
"isCorrect": true
},
{
"id": 10,
"entitled": "Non",
"isCorrect": false
}
]
}
],
"pageCount": 3,
"next": "http://localhost:3000/v1/api/poll/40/question?page=1"
}
您需要像这样添加['results']
:
List pollsList = jsonDecode(jsonstr)['results'];
答案 1 :(得分:0)
您需要像这样将json['answers']
投射到List
if (json['answers'] != null) {
answers = (json['answers'] as List).map((v) => Answer.fromJson(v)).toList();
}
除非有特殊要求,否则最好使用https://pub.dev/packages/json_annotation
而不是手动实现toJson
和fromJson
。
我能够解析以下字符串
[
{
"id": 1,
"entitled": "yes",
"difficulty": 1,
"answers": [
{
"value":"a"
}
]
}
]
使用
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;
}
@override String toString() => toJson().toString();
static List<PollQuestionsDto> arrayOfQuestionsFromJson(String json) {
return (jsonDecode(json) as List).map((e) => PollQuestionsDto.fromJson(e)).toList();
}
}
这是您所期望的吗(当我将Anser类与一个变量value
一起使用时,您可能必须更改Answer的json)?