Dart 包含项的映射中的Return语句:在flift DropdownButton中,它为什么工作以及如何工作

Dart 包含项的映射中的Return语句:在flift DropdownButton中,它为什么工作以及如何工作,dart,flutter,dropdownbutton,Dart,Flutter,Dropdownbutton,我对飞舞和飞镖还不熟悉。我遵循的是一个免费教程,但我很困惑在map in items(在DropdownButton中)中是如何有一个返回语句的。这是怎么回事?我希望澄清返回语句为什么存在,以及它的值发送到哪里 我曾试图查找返回语句在映射中的位置,但在如何提问的问题上,我可能弄错了。代码按照给定的方式工作,但我不确定它是如何工作的。是否有一个逐步简化的代码形式,可能导致更多的理解。正如现在所说的“它在我头上” 下拉按钮( 项目:_currences.map( (字符串下拉字符串){ //对这份退

我对飞舞和飞镖还不熟悉。我遵循的是一个免费教程,但我很困惑在map in items(在DropdownButton中)中是如何有一个返回语句的。这是怎么回事?我希望澄清返回语句为什么存在,以及它的值发送到哪里

我曾试图查找返回语句在映射中的位置,但在如何提问的问题上,我可能弄错了。代码按照给定的方式工作,但我不确定它是如何工作的。是否有一个逐步简化的代码形式,可能导致更多的理解。正如现在所说的“它在我头上”

下拉按钮(
项目:_currences.map(
(字符串下拉字符串){
//对这份退货声明感兴趣吗
返回下拉菜单项(
值:dropDownStringItem,
子项:文本(dropDownStringItem),
);
}
).toList(),//转换为列表
onChanged:(字符串newValueSelected){
_onDropDownItemSelected(newValueSelected);
},
值:_currentItemSelected,
),

对于新来的人来说,命名可能会让人困惑,但让我为您解释一下您发布的代码:

因此
DropdownButton
构造函数期望一个
DropdownMenuItem
列表作为参数,但在您的情况下,您没有
DropdownMenuItem
列表,而是有一个
字符串的列表。您需要的是将字符串转换为
下拉菜单项
,最简单的方法是对字符串执行
操作,为每个
字符串创建一个新的
下拉菜单项
,将其添加到列表中,然后将其发送到
下拉按钮
。比如:

List<DropdownMenuItem> newGenerated = [];
_currencies.forEach((value) {
    DropdownMenuItem newItem = DropdownMenuItem<String>(
                                  value: dropDownStringItem,
                                  child: Text(dropDownStringItem),
                                 );
    newGenerated.add(newItem)
}
 DropdownButton<String>(
        items: newGenerated,  //convert to List
        onChanged: (String newValueSelected) {
          _onDropDownItemSelected(newValueSelected);
        },
        value: _currentItemSelected,
      ),
dropDownStringItem
将获取
\u货币中每个元素的值。
返回下拉菜单项…
将返回转换后的对象


希望这能有所帮助。

对于新来的人来说,命名可能会让人困惑,但让我为您解释一下您发布的代码:

因此
DropdownButton
构造函数期望一个
DropdownMenuItem
列表作为参数,但在您的情况下,您没有
DropdownMenuItem
列表,而是有一个
字符串的列表。您需要的是将字符串转换为
下拉菜单项
,最简单的方法是对字符串执行
操作,为每个
字符串创建一个新的
下拉菜单项
,将其添加到列表中,然后将其发送到
下拉按钮
。比如:

List<DropdownMenuItem> newGenerated = [];
_currencies.forEach((value) {
    DropdownMenuItem newItem = DropdownMenuItem<String>(
                                  value: dropDownStringItem,
                                  child: Text(dropDownStringItem),
                                 );
    newGenerated.add(newItem)
}
 DropdownButton<String>(
        items: newGenerated,  //convert to List
        onChanged: (String newValueSelected) {
          _onDropDownItemSelected(newValueSelected);
        },
        value: _currentItemSelected,
      ),
dropDownStringItem
将获取
\u货币中每个元素的值。
返回下拉菜单项…
将返回转换后的对象


希望这有帮助。

如果其中一个答案对您有用,您应该接受一个。谢谢如果其中一个答案对你有用,你应该接受一个。谢谢
 (String dropDownStringItem) {

                   // interested in this return statement 
                  return DropdownMenuItem<String>(
                    value: dropDownStringItem,
                    child: Text(dropDownStringItem),
                  );