DEV Community

Md.Mahabub Al Islam
Md.Mahabub Al Islam

Posted on

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...

Top comments (0)