繁体   English   中英

Flutter:DropDownMenuItem - 如何访问 i 或 index

[英]Flutter: DropDownMenuItem - How to access i or index

我正在尝试访问我的计数器变量i或当前项目的当前索引,但这似乎是不可能的。

items: listeZutatenDropdown[i].map((String value) {     
                                  return DropdownMenuItem(
                                    value: value,
                                    child: (value == "Zutat auswählen") ? new Text("Zutat auswählen") : new Text(value + " " + i.toString()),
                                  );
                                }).toList()

如果我像在代码示例中那样打印 i ,我总是得到 0。(这与 return 语句的范围有关吗?)。

如果我尝试使用索引作为附加参数,我得到“参数类型'List'不能分配给参数类型'List<DropdownMenuItem>?'”和“参数类型'List'不能分配给参数类型 'List<DropdownMenuItem>?'.dart(argument_type_not_assignable) 参数类型 'DropdownMenuItem Function(int, String)' 不能分配给参数类型 'dynamic Function(String)'” 这让我很困惑,因为我看到了人们一直在使用 .map() 方法和 .forEach() 方法,但是当我想使用它们时......^^

最后,我尝试将 i 保存在一个变量中,并在我的 return 语句中访问这个变量。 与之前相同的结果 (0)。

希望你能告诉我我做错了什么! 期待您的反馈!

问候托马斯

由于我无法访问我的计数器变量或索引,我只是复制了我已经为我的 listZutatenDropdown 拥有的代码

listeZutatenDropdown = List<List<String>>.generate(listZutaten.length, (i) => List<String>.generate(listZutaten.length, (i) => listZutaten[i]));

并添加了单位列表

listeZutatenUndEinheiten = List<List<String>>.generate(listZutaten.length, (i) => List<String>.generate(listZutaten.length, (i) => listZutaten[i] + " (" + listEinheiten[i] + ")"));

在我看来,这不是最干净的方式,因为我更愿意使用计数器变量或索引来减少不必要的代码,但它确实有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM