Reputation: 657
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#
Upvotes: 1
Views: 1106
Reputation: 657118
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()
Upvotes: 2
Reputation: 51682
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();
}
Upvotes: 1
Reputation: 276911
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";
}
Upvotes: 1