flutter - 从颤动的列表中获取值
问题描述
我正在使用一些虚拟数据在颤振上构建 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'],
),
];
我如何访问这些数据?我还在学习颤振,指出我所缺少的会有所帮助
解决方案
- 首先,你应该在你的类中添加“require” 。否则 dart 会给你类似下一个错误:
第 51 行 • 参数“movieName”的值不能为“null”,因为它的类型,但隐含的默认值为“null”。(查看文档)尝试添加显式的非“空”默认值或“必需”修饰符。
在需要的地方导入类和示例信息。
导入'包:exampleapp/movie_list_sample_information.dart;
导入'包:exampleapp/movies.dart;
使用变量获取信息
movieLists[0].movieName // 获取列表中的第一部电影
下一个 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
推荐阅读
- git - GIT: post-receive -> 获取推送的分支名
- java - 字符串之间的 Java 模式正则表达式搜索
- replace - 记事本++替换功能
- java - 在“mouseDragged”时保存第一个鼠标位置
- r - R + Shiny:将上传的数据集保存到列表/从列表项中选择以查看
- ios - 在另一个 Observable Bool 变量中获取 Observable Bool 变量的结果?
- android - 在 Kotlin 中对 json 中的数据进行排序
- android - 序列号或 IMEI 在 android 中已更改
- javascript - 使用 PHP 的 Select2 optgroup 格式
- css - WordPress下拉菜单水平而不是垂直