json - Flutter:如何显示包含对象列表的已解析json对象中的数据

标签 json flutter dart

从assets文件夹解析本地JSON对象后,我无法在屏幕上显示内容,我已经呆了两天,请记住,我是编程新手,开发工作从类(class),这是我的第一个项目。我很感激我能得到的任何帮助。

我的JSON文件

{
 "movies": [
  {
   "name": "JOKER",
   "actors": [
     {
       "name1": "Joaquin Phoenix"
     },
     {
       "name2": "Robert De Niro"
     },
     {
      "name3": "Zazie Beetz"
     }
  ],
   "category": [
     {
      "genre1": "Crime"
     },
     {
      "genre2": "Drama"
     },
     {
      "genre3": "Thriller"
     }
   ],
   "timeM": "121",
   "text": "A gritty character study of Arthur Fleck, a man disregarded by society.",
   "image": "image url",

 ***there are 4 more similar objects within the movies list***
},

我的JSON模型类别
import 'dart:convert';

MovieList movieListFromJson(String str) => MovieList.fromJson(json.decode(str));

class MovieList {
    List<Movie> movies;

    MovieList({ this.movies,});

    factory MovieList.fromJson(Map<String, dynamic> json) => MovieList(
        movies: List<Movie>.from(json["movies"].map((x) => Movie.fromJson(x))),
    );
}

class Movie {
    String name;
    List<Actor> actors;
    List<Category> category;
    String timeM;
    String text;
    String image;

    Movie({ this.name, this.actors, this.category, this.timeM, this.text, this.image,});

    factory Movie.fromJson(Map<String, dynamic> json) => Movie(
        name: json["name"],
        actors: List<Actor>.from(json["actors"].map((x) => Actor.fromJson(x))),
        category: List<Category>.from(json["category"].map((x) => Category.fromJson(x))),
        timeM: json["timeM"] == null ? null : json["timeM"],
        text: json["text"],
        image: json["image"],
    );
}

class Actor {
    String name1;
    String name2;
    String name3;

    Actor({this.name1,this.name2,this.name3,
    });

    factory Actor.fromJson(Map<String, dynamic> json) => Actor(
        name1: json["name1"] == null ? null : json["name1"],
        name2: json["name2"] == null ? null : json["name2"],
        name3: json["name3"] == null ? null : json["name3"],
    );
}

class Category {
    String genre1;
    String genre2;
    String genre3;

    Category({this.genre1,this.genre2,this.genre3,
    });

    factory Category.fromJson(Map<String, dynamic> json) => Category(
        genre1: json["genre1"] == null ? null : json["genre1"],
        genre2: json["genre2"] == null ? null : json["genre2"],
        genre3: json["genre3"] == null ? null : json["genre3"],
    );
}

我的小工具类
class App extends StatefulWidget {
@override
_AppState createState() => _AppState();
}

class _AppState extends State<App> {
var movies;

void loadAsset() async {
  final response = await rootBundle.loadString('assets/movies.json');
  final movieModel = movieListFromJson(response);

  setState(() {
    movies = movieModel;
  });
}

_AppState() {
  loadAsset();
}

@override
Widget build(BuildContext context) {
  return MaterialApp(
    debugShowCheckedModeBanner: true,
    home: Scaffold(
    body: Text(movies[2].category[1].genre1),
    appBar: AppBar(
      title: Text('movie schedules'),
      ),
    ),
  );
}

}

最佳答案

从新手到新手
gitrepo给你,我的 friend 喜欢:) https://github.com/nitinsingh9x/flutterFutureBuilder

class _AppState extends State<App> {
  @override
  Widget build(BuildContext context) {
    `return FutureBuilder(`
      future: rootBundle.loadString('assets/movies.json'),
      builder: (BuildContext context, AsyncSnapshot snap) {
        if (snap.hasData) {
          var movies = movieListFromJson(snap.data);
          return Scaffold(
              appBar: AppBar(
                  backgroundColor: Colors.deepPurple, title: Text('Movies')),
              body: Column(
                children:
                    movies.movies.map((movie) => Text(movie.name)).toList(),
              ));
        } else {
          return CircularProgressIndicator();
        }
      },
    );
  }
}

enter image description here

关于json - Flutter:如何显示包含对象列表的已解析json对象中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58269174/

相关文章:

ios - 错误 : Build input file cannot be found: '/Users/GoogleService-Info.plist' (in target 'Runner' from project 'Runner' )

flutter - 在 flutter 中使用步进器实现注册表单时如何修复 "[_LocalizationsScope-[GlobalKey#27fdc], _InheritedTheme])"错误?

javascript - 如何在 flutter 中将一个图标更改为两个不同的图标?

canvas - 在 Canvas 上进行交互式控件的能力

dart - web-ui 2路数据绑定(bind)

flutter - 如何在自定义文本字段中添加OnChange

javascript - 将键分配给数组对象

c# - C# 中的 Google Geocoding Json 解析问题

c# - 如何去除json字符串中的空值

ios - 使用 AFNetworking 3.0 在 URL 正文中发送 JSON 数据