发生异常。 _TypeError(类型'List <dynamic>'不是类型'Map <String,dynamic>'的子类型)请帮助我

时间:2020-10-25 03:02:53

标签: flutter dart

Blockquote

enter image description here

enter image description here

我开始研究json并遇到了这样的问题。 api从不飞溅。抱歉,如果不清楚,我是第一次将问题留在这里

`import 'dart:convert';

import 'package:http/http.dart' as http;

class OfficesList {
List<Office> offices;
OfficesList({this.offices});

factory OfficesList.fromJson(Map<String, dynamic> json) {
var officesJson = json['self'] as List;

List<Office> officesList = officesJson.map((i) => Office.fromJson(i));

return OfficesList(
  offices: officesList,
);
}
}

class Office {
final String username;
final String createdAt;
final String image;

Office({this.username, this.createdAt, this.image});

factory Office.fromJson(Map<String, dynamic> json) {
return Office(
    username: json['updated_ad'] as String,
    createdAt: json['created_at'] as String,
    image: json['id'] as String);
}
}

Future<OfficesList> getOfficesList() async {
const url =
  'https://api.unsplash.com/photos? 
client_id=6L1WckRABJLdhkHch6iFN9qMhPaUVQKtyPuCw90wZdI';
final response = await http.get(url);
if (response.statusCode == 200) {
return OfficesList.fromJson(json.decode(response.body));
} else {
throw Exception('Error: ${response.reasonPhrase}');
}
}`

1 个答案:

答案 0 :(得分:0)

您将收到GameEngine的列表作为api调用的响应,但是您正在解码它,就像收到一个一样。尝试使用以下代码来解析api响应:

Office

请告诉我是否可以解决您的问题。