用键和值从地图中创建一个小部件列表



如果我说SimpleDialog()并且接受children[],并且我想从Map<int, String>填充它,我该怎么做?我需要密钥和价值来创建子小部件。

const optionsMap = {
    111:"First option",
    222:"Second option",
}
return SimpleDialog(
    title: Text('Choose one'),
    children: // I don't know what to put here
              // I want to have widgets based on optionsMap keys and values
)

我通过在返回语句上方创建List<Widget>来解决它,但仅仅是为了方便(并且在Dart中变得更好),我想知道是否有一种内联方法。

更新答案,以结合@lrn的评论

您可以使用map

const optionsMap = {
    111:"First option",
    222:"Second option",
}     
    return SimpleDialog(
        title: Text('Choose one'),
        children: optionsMap.entries.map((entry) {
          var w = Text(entry.value);
          doSomething(entry.key);
          return w;
        }).toList());
;

相关内容

最新更新