从颤振中的列表中获取值

时间:2021-05-11 19:49:29

标签: flutter flutter-layout

我正在使用一些虚拟数据在 flutter 上构建 UI。我建模了一个名为电影的类

class Movies {
  final String movieName;
  final int rating;
  final String imgUrl;
  final String desc;
  final String runTime;
  final String language;
  final List<String> genre;

  Movies(
      {this.movieName,
      this.rating,
      this.imgUrl,
      this.desc,
      this.runTime,
      this.language,
      this.genre});
}

并创建了一个数据列表

List<Movies> movielists = [
  Movies(
    movieName: 'Karnan',
    language: 'Tamil',
    rating: 3,
    imgUrl:
        'https://www.filmibeat.com/ph-big/2021/04/karnan-movie-posters_16179793174.jpg',
    desc:
        'Karnan, a fearless village youth, must fight for the rights of the people of his village. The people of the village belong to a marginalized community that has been oppressed for decades, mainly by the dominant caste groups in the region aided by the government officials and the police who intend to keep them suppressed under their shoes',
    runTime: '143.2',
    genre: ['Thriller', 'Crime'],
  ),
  Movies(
    movieName: 'Wrath Of Man',
    language: 'English',
    rating: 4,
    imgUrl:
        'https://cdn.flickeringmyth.com/wp-content/uploads/2021/03/Wrath-of-Man.jpg',
    desc:
        'Patrick Hill begins work at Fortico Security, an armored truck company. After being commended by the superior Terry for his references, he is introduced to Bullet, who nicknames him "H" and oversees his training. H gets off to a rocky start with his colleagues, particularly Boy Sweat Dave, over his mysterious nature',
    runTime: '94.8',
    genre: ['Action', 'Thriller'],
  ),
  Movies(
    movieName: 'GodZilla vs Kong',
    language: 'English',
    rating: 5,
    imgUrl:
        'https://cdn.flickeringmyth.com/wp-content/uploads/2021/03/Godzilla-vs-Kong-Dolby-600x889.jpg',
    desc:
        'Five years after Godzilla defeated King Ghidorah,[c] Kong is monitored by Monarch within a giant dome on Skull Island. Kong is visited by Jia, the last Iwi native and Kong expert Ilene Andrews',
    runTime: '94.8',
    genre: ['Action', 'Thriller'],
  ),
];

我如何访问这些数据?我还在学习颤振,指出我所缺少的会有所帮助

1 个答案:

答案 0 :(得分:1)

  1. 首先,您应该在类中添加 "require"。否则 dart 会给你一个与下一个类似的错误:
<块引用>

第 51 行 • 参数“movieName”的值不能为“null” 由于其类型,但隐式默认值为“null”。 (看法 docs) 尝试添加一个明确的非“空”默认值或 “必需”修饰符。

  1. 在需要的地方导入类和示例信息。

    import 'package:exampleapp/movie_list_sample_information.dart;

    import 'package:exampleapp/movies.dart;

  2. 使用变量获取信息

    movieLists[0].movi​​eName // 获取列表中的第一部电影

下一个 dartpad 使用您的代码作为示例:https://dartpad.dev/95d67aa68267296ac3fd8a56405b2880?null_safety=true

按“运行”按钮,您应该会在“控制台”中看到第一部电影的名称

enter image description here

编辑:

要在颤振中动态读取列表,您可以使用 ListView.builder

ListView.builder(
          itemCount: movieLists.length,
          itemBuilder: (context, index) {
            final Movies movies = movieLists[index];
            return ListTile(
              title: Text('${movies.movieName}'),
            );
          },
        );

新飞镖:https://dartpad.dev/flutter?null_safety=true&id=833b53af6b75b64d751381092dbe07ed

ListView.builder