首页 > 解决方案 > 从颤动的列表中获取值

问题描述

我正在使用一些虚拟数据在颤振上构建 UI。我已经建模了一个名为 movies 的类

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'],
  ),
];

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

标签: flutterflutter-layout

解决方案


  1. 首先,你应该在你的类中添加“require” 。否则 dart 会给你类似下一个错误:

第 51 行 • 参数“movieName”的值不能为“null”,因为它的类型,但隐含的默认值为“null”。(查看文档)尝试添加显式的非“空”默认值或“必需”修饰符。

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

    导入'包:exampleapp/movie_list_sample_information.dart;

    导入'包:exampleapp/movies.dart;

  2. 使用变量获取信息

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

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

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

在此处输入图像描述

编辑:

要在颤动中动态读取列表,您可以使用 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


推荐阅读