如果我说了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
)
我通过在return语句上方预先创建一个List<Widget>
来解决它,但是为了方便起见(在Dart中变得更好),我想知道是否有一种内联的方法。
答案 0 :(得分:1)
更新答案以包含@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());
;