How can I get all elements from the map, if I need to use index without listView.builder?
I created the dropdown menu, and as I inderstood it creates the list with map()
method for DropDownItem() widget. I am trying to use it, but get raw string data and it shouldn't be so. Also API is working on websockets and it returns Map like this {2: GMT 02:00, 3: GMT 03:00}
, (it returns Object but I need to use as String)
So when I trying to put it in the UI without raw strings from API, I do it like this:
items: snapshot.data?.values
.map((e) => DropdownMenuItem<String>(
value: e,
child:
Text(snapshot.data?.values.elementAt(index) ?? '')))
.toList(),
So the question is, how can I use elementAt()
method and put in it index
without creating listview.builder to get the index of element? May be should I manipulate with entries
? I was trying to create int? index
, but it doesen't work.
If it is important, the length of elements is 11.
CodePudding user response:
items: snapshot.data?.values
.map((e) => DropdownMenuItem<String>(
value: e,
child:
Text(snapshot.data?.values.elementAt(index) ?? '')))
.toList(),
Here e
by itself is a single entry from the value. so you can use the same value here in child too like unless you wish to access index of some other element.
items: snapshot.data?.values
.map((e) => DropdownMenuItem<String>(
value: e,
child:
Text(e.toString()??"")))
.toList(),
CodePudding user response:
You can use List.generate
DropdownButton(
items: List.generate(values.length, (index) => ..),
Or for loop
DropdownButton(
items: [
for (int i = 0; i < values.length; i )
DropdownMenuItem(child: child)
],