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
)
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.
Well, Keys are the ones to preserve state when widgets move around the widget tree. It is used to preserve the user scroll location or keeping state when modifying a collection.
Update answer to incorporate @lrn 's comment
You can use map
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());
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With