Dart 省道(颤振)贴图。贴图没有toList()
我有一个映射结构,我想为每个条目创建一个下拉菜单项。我想做的就是这样打电话给.map.toListDart 省道(颤振)贴图。贴图没有toList(),dart,Dart,我有一个映射结构,我想为每个条目创建一个下拉菜单项。我想做的就是这样打电话给.map.toList var _languages = { 'en': 'English You cannot call a .map directly in a scenario like yours as returned type is a Map<K, V>, means is a reply of the original but transformed (if you need to tra
var _languages = {
'en': 'English You cannot call a .map
directly in a scenario like yours as returned type is a Map<K, V>
, means is a reply of the original but transformed (if you need to transform it). In your scenario you need another kind of map that is an Iterable<dynamic>
. To have that you have to pass by .entries
which gives you a Iterable<MapEntry<K, V>>
and then call on that .map
which gives you a Iterable<T>
where the returned T
can be even pizza
:).
here a small piece of code that helps you understand the concept:
final Map<String, String> _languages = {
'en': 'English Map<K, V>.map
returns another Map
, which isn't what you want.
You instead can create an Iterable
from the Map
first, and use Iterable.map
, which returns another Iterable
:
var menuItems = languages.entries
.map((mapEntry) =>
DropdownMenuItem(value: mapEntry.key, child: Text(mapEntry.value)))
.toList();
var_语言={
“en”:“English在像您这样的场景中,您不能直接调用.map,因为返回的类型是map,意思是原始的回复,但如果您需要转换它,它会被转换。在您的场景中,您需要另一种类型的map,它是Iterable。要拥有它,您必须经过。提供Iterable的条目,然后调用它。map which给出了一个Iterable,其中返回的T可以是偶数:
下面是一小段代码,可以帮助您理解这个概念:
最终地图_语言={
'en':'English返回另一个地图,这不是您想要的
您可以先从映射创建一个Iterable,然后使用,这将返回另一个Iterable:
var menuItems=languages.entries
.MapEntry=>
DropdownMenuItemvalue:mapEntry.key,子项:TextmapEntry.value
托利斯特先生;
或者:
变量菜单项=[
用于语言中的var mapEntry.entries
DropdownMenuItemvalue:mapEntry.key,子项:TextmapEntry.value,
];
映射方法om map与iterables(如List)上的映射方法不同。它返回的不是一个Iterable,而是一个map。因此,内部方法需要返回一个MapEntry,以便该方法可以构造一个要返回的新映射对象。要将其转换为列表,需要将映射本身转换为列表
我假设您要做的是将映射中的条目映射到DropDownButton,其中语言代码是按钮的值,语言文本是按钮的文本。因此,您希望调用_langages.entries上的map,这将为您提供映射中所有键和值的Iterable。然后,您可以在此Iterable上调用map它将实现您所期望的:
var_语言={
是的,地图在Dart中似乎是模棱两可的。我想要的是原始含义,即将每个项目转换为新项目,而不是创建数据structure@WouterVandenputtemap方法是Iterable专有的。map类不是Iterable。