【发布时间】:2019-02-12 20:16:13
【问题描述】:
很好,我有以下问题我有一个以下 http:// 的 json,我有一个类来获取使用 https://app.quicktype.io/ 的数据,代码如下
// To parse this JSON data, do
//
// final moviesFirstLoad = moviesFirstLoadFromJson(jsonString);
import 'dart:convert';
MoviesFirstLoad moviesFirstLoadFromJson(String str) {
final jsonData = json.decode(str);
return MoviesFirstLoad.fromJson(jsonData);
}
String moviesFirstLoadToJson(MoviesFirstLoad data) {
final dyn = data.toJson();
return json.encode(dyn);
}
class MoviesFirstLoad {
List<Movierecent> movierecent;
MoviesFirstLoad({
this.movierecent,
});
factory MoviesFirstLoad.fromJson(Map<String, dynamic> json) => new MoviesFirstLoad(
movierecent: new List<Movierecent>.from(json["movierecent"].map((x) => Movierecent.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"movierecent": new List<dynamic>.from(movierecent.map((x) => x.toJson())),
};
}
class Movierecent {
int id;
String movieId;
String title;
String genre;
String myear;
String released;
String runtime;
String rated;
String director;
String actors;
String plot;
String imdbrating;
String type;
String production;
int internalid;
String poster;
Movierecent({
this.id,
this.movieId,
this.title,
this.genre,
this.myear,
this.released,
this.runtime,
this.rated,
this.director,
this.actors,
this.plot,
this.imdbrating,
this.type,
this.production,
this.internalid,
this.poster,
});
factory Movierecent.fromJson(Map<String, dynamic> json) => new Movierecent(
id: json["id"],
movieId: json["movieID"],
title: json["title"],
genre: json["genre"],
myear: json["myear"],
released: json["released"],
runtime: json["runtime"],
rated: json["rated"],
director: json["director"],
actors: json["actors"],
plot: json["plot"],
imdbrating: json["imdbrating"],
type: json["type"],
production: json["production"],
internalid: json["internalid"],
poster: json["poster"],
);
Map<String, dynamic> toJson() => {
"id": id,
"movieID": movieId,
"title": title,
"genre": genre,
"myear": myear,
"released": released,
"runtime": runtime,
"rated": rated,
"director": director,
"actors": actors,
"plot": plot,
"imdbrating": imdbrating,
"type": type,
"production": production,
"internalid": internalid,
"poster": poster,
};
}
现在第一个标签显示我应该使用
final moviesFirstLoad = moviesFirstLoadFromJson(jsonString);
因此我有以下内容,在这里我不知道该怎么做,因为访问数据以将它们放在列表中就像
Future<List<Movierecent>> loadMovies() async {
final response = await http.get("http://emovies.evolucionone.com/");
if (response.statusCode == 200){
final moviesFirstLoad = moviesFirstLoadFromJson(response.body);
return moviesFirstLoad.movierecent;
}else{
throw Exception ('Failed to load Data');
}
}
如果有人帮助我,我需要帮助来获取 json 的数据我已经阅读了几个主题,但它们都不适合我...
【问题讨论】:
-
moviesFirstLoad的类型为MoviesFirstLoad。moviesFirstLoad.movierecent应该给你电影列表(Movierecent类型)。所以只需将return moviesFirstLoad.movierecent;添加到 if 块中。 -
不工作错误说返回类型 'List
' 不是方法 'loadMovies' 定义的'Future - >'。
-
您可能想要添加响应,例如错误是什么。
-
return moviesFirstLoad.movierecent;这告诉我这个错误The return type 'List<Movierecent>' isn't a 'Future<List<MoviesFirstLoad>>', as defined by the method 'loadMovies'以及我如何在列表中得到结果 -
如果您需要获取电影列表,请将返回类型更改为
Future<List<Movierecent>>,或者如果您想要完整的对象将其更改为Future<MoviesFirstLoad>并返回moviesFirstLoad。