DEV Community

Md.Mahabub Al Islam
Md.Mahabub Al Islam

Posted on

3 2

Bind list of selected value in DropdownButton

I've a dropdown button inside a listview builder. User will upload multiple files and will select their different types using dropdown button. Listview length will be according to the number of selected files.

List<String> _selectedItem = [];
DropdownButton(
  hint: Text('Select type'),
  isExpanded: true,
  underline: Container(),
  value: _selectedItem[index].isNotEmpty ? _selectedItem[index] : null,
  items: _itemList.map((e) {
    return DropdownMenuItem(
      child: Text(e.description!),
      value: e.code,
    );
  }).toList(),
  onChanged: (value) {
    setState(() {
      _selectedItem[index] = value.toString();
    });
  })
Enter fullscreen mode Exit fullscreen mode

But while I'm using list to get the selected value, it'll throws error Error : RangeError (index): Invalid value: Valid value range is empty: 0.

ListView builder:

ListView.separated(
  itemCount: fileList.length == 0 ? 1 : fileList.length,
  separatorBuilder: (BuildContext context, int index) {
   return SizedBox(height: 8.h);
  },
  shrinkWrap: true,
  physics: NeverScrollableScrollPhysics(),
  itemBuilder: (context, index) {
// Other code
Enter fullscreen mode Exit fullscreen mode

Here, fileList is number of selected files. Initially it'll be 0 that's why I'm setting it 1 so that user can see the Select File UI. Later the listview will generated according to number of selected files. For example, user selects 2 files. Then there'll be 2 card and each card will contain File type dropdown, file name and comment.
How could I resolve this issue? Need Help...

Sentry mobile image

App store rankings love fast apps - mobile vitals can help you get there

Slow startup times, UI hangs, and frozen frames frustrate users—but they’re also fixable. Mobile Vitals help you measure and understand these performance issues so you can optimize your app’s speed and responsiveness. Learn how to use them to reduce friction and improve user experience.

Read full post →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more