使用键和值从地图创建小部件列表 [英] Creating a list of widgets from map with keys and values

查看:52
本文介绍了使用键和值从地图创建小部件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I have say a SimpleDialog() and it accepts children[] and I want to populate it from a Map<int, String>, how would I do that? I need both the key and value to create the child widget.

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中变得更好)想知道是否有一种内联的方法。

I solved it by creating a List<Widget> beforehand above the return statement, but just for convenience (and becoming better in Dart) I'd like to know if there's a way to do it inline.

推荐答案

更新答案以包含@lrn的评论

Update answer to incorporate @lrn 's comment

您可以使用地图

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());
;

这篇关于使用键和值从地图创建小部件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆