gpt4 book ai didi

dart - C# Yield Return on Dart/Flutter Return List 循环

转载 作者:IT王子 更新时间:2023-10-29 06:50:00 26 4
gpt4 key购买 nike

我有以下方法:

List<DropdownMenuItem<String>> _buildGitIgnoreTemplateItems() {
var _dropDownMenuItems = List<DropdownMenuItem<String>>();

_gitIgnoreTemplateNames.forEach((templateName) {
_dropDownMenuItems.add(DropdownMenuItem(
child: Text(templateName),
value: templateName,
));
});

return _dropDownMenuItems;
}

我想要实现的是删除变量 _dropDownMenuItems,例如:

List<DropdownMenuItem<String>> _buildGitIgnoreTemplateItems() {
_gitIgnoreTemplateNames.forEach((templateName) {
**yield return** DropdownMenuItem(
child: Text(templateName),
value: templateName,
);
});
}

您可以在其他语言中看到类似的实现,例如:C#

最佳答案

C# 太早了,但它看起来像 Synchronous generators

Iterable<DropdownMenuItem<String>> _buildGitIgnoreTemplateItems() sync* {
for(var templateName in _gitIgnoreTemplateNames) {
yield DropdownMenuItem(
child: Text(templateName),
value: templateName,
);
}
}

但也许你只是想要

_gitIgnoreTemplateNames.map((templateName) => 
DropdownMenuItem(
child Text(templateName),
value: templateName)
).toList()

关于dart - C# Yield Return on Dart/Flutter Return List<String> 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51358988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com