I have the following method:
List<DropdownMenuItem<String>> _buildGitIgnoreTemplateItems() {
var _dropDownMenuItems = List<DropdownMenuItem<String>>();
_gitIgnoreTemplateNames.forEach((templateName) {
_dropDownMenuItems.add(DropdownMenuItem(
child: Text(templateName),
value: templateName,
));
});
return _dropDownMenuItems;
}
What i am trying to achived is remove the variable _dropDownMenuItems
something like:
List<DropdownMenuItem<String>> _buildGitIgnoreTemplateItems() {
_gitIgnoreTemplateNames.forEach((templateName) {
**yield return** DropdownMenuItem(
child: Text(templateName),
value: templateName,
);
});
}
You can see similar implementation in other languages like: C#
C# is way too long ago, but it looks like Synchronous generators
Iterable<DropdownMenuItem<String>> _buildGitIgnoreTemplateItems() sync* {
for(var templateName in _gitIgnoreTemplateNames) {
yield DropdownMenuItem(
child: Text(templateName),
value: templateName,
);
}
}
but perhaps you just want
_gitIgnoreTemplateNames.map((templateName) =>
DropdownMenuItem(
child Text(templateName),
value: templateName)
).toList()
The equivalent in dart is with Stream
and StreamController
for async. And Iterable
for sync. You can create them manually or using custom function with async*
or sync*
keywords
Iterable<String> foo() sync* {
yield "Hello";
}
Stream<String> foo() async* {
yield "Hello";
}
Dart has a simpler syntax to achieve what you want:
List<DropdownMenuItem<String>> _buildGitIgnoreTemplateItems() {
return _gitIgnoreTemplateNames
.map((g) => DropdownMenuItem(
child: Text(g),
value: g,
))
.toList();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.