颤抖如何获取json的数据

时间:2019-02-12 20:16:14

标签: json dart flutter

好,我遇到以下问题,我在以下http://emovies.evolucionone.com/之后有一个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的数据,我已经阅读了几个主题,但是没有一个适合我...

2 个答案:

答案 0 :(得分:0)

loadMovies()返回Future<List<Movierecent>>,这是未来。如果您想要电影的基本列表,则可以进行

之类的操作
loadMovies().then((List<Movierecent> movieList) {
 /* do what you want to do here like invoking setState()....*/
}.catchError((e) {
/* Handle Error scenario here */
};

您可能想参考Dart documentation of Futures

答案 1 :(得分:0)

我自己回答我的问题

这是从json获取数据


Future<MoviesFirstLoad> loadMovies() async {
  final Response response = await http.get(dogApiUrl);
  //final List<Movierecent> posterimage = List<Movierecent>();
  if (response.statusCode == 200){
    //final responsejson = json.decode(response.body);
    final moviesFirstLoad = moviesFirstLoadFromJson(response.body);
    // moviesFirstLoad.movierecent.forEach((poster) => posterimage.add(poster));
    print(moviesFirstLoad);
    return moviesFirstLoad;
  }else{
    throw Exception ('Failed to load Data');
  }
}

以显示列表中的数据

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Movies')),
      body: FutureBuilder(
        future: loadMovies(),
        builder: (BuildContext context, AsyncSnapshot<AppData> snapshot) {
          if (!snapshot.hasData) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
          return ListView.builder(
            itemCount: snapshot.data.movierecent.length,
            itemBuilder: (BuildContext context, int index) {
              final Movierecent movie = snapshot.data.movierecent[index];
              return ListTile(
                title: Text(movie.title),
                subtitle: Text(movie.genre),
              );
            },
          );
        },
      ),
    );
  }
}