list - Flutter: generic list handles different key, value pairs
问题描述
I have a class that has a generic List as property, that I have to initialize, and that has a function that returns a card using that list:
class buildCard{
buildCard(this.list);
final List list;
Widget buildCard(int position) {
return Card(child: ListTile(title: list[position].name,),);
}
Now, as you can see, I use
list[position].name
that works for the majority of the stuff that I have, but, in case I will have a list which doesn't have "name" as key, I will have some troubles. How can I avoid this problem?
解决方案
You can use is
to check the type
Widget buildCard() {
if(list[position].name is List) {
return Card(child: ListTile(title: list[position][0].name,),); // or similar - I don't know your exact structure
} else {
return Card(child: ListTile(title: list[position].name,),);
}
}
推荐阅读
- java - 配置 Wildfly 以使用 pom.xml 中的 resteasy,但不使用 Wildfly 模块
- machine-learning - 如何从文档中提取特征
- windows - 在for循环批处理脚本(windows,批处理脚本)中的路径中的findstr命令变量扩展
- pandas - Pandas 列级别每行时间戳之间的差异
- python - Snakemake - 尝试使用 global_wildcards 时出现问题(TypeError:预期的 str,得到列表)
- sapui5 - SAP Cloud Platform Deployment - neo-app.json 中的无效内容 (?)
- regex - 角度6中的嵌套验证?
- azure - Azure Cosmos DB SQL Like,准备好的语句
- excel - 出现错误 1004 时如何添加消息框
- flutter - 在列表视图中添加滚动控制器时,CustomScrollView 的浮动和捕捉行为不起作用