Blockquote
我开始研究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}');
}
}`
答案 0 :(得分:0)
您将收到GameEngine
的列表作为api调用的响应,但是您正在解码它,就像收到一个一样。尝试使用以下代码来解析api响应:
Office
请告诉我是否可以解决您的问题。