Solution 1 :
In this line the length is 0 but you assigned it as 1. so there is no value at index 0..
itemCount: fileList.length == 0 ? 1 : fileList.length,
change it to
itemCount: fileList.length,
Solution 2 :
it’s because _selectedItem[index]
doesn’t yet exists when you first call it. _selectedItem is instantiated as an empty list (length of 0). so you can’t assign something to index 3 (for example).
if you just want to have a list with the selected items you’ll have to do a check for that:
if(_selectedItem.contains(value.toString()) return;
setState(() => _selectedItem.add(value.toString());
or if you want your dropdowns to be a toggle instead:
_selectedItem.contains(value)
? _selectedItem.remove(value)
: _selectedItem.add(value)
Problem :
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();
});
})
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
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 store the selected value into list and bind and display it into DropdownButton? Need help to get out from this issue…
Comments
Comment posted by Michael Horn
Is there a reason
Comment posted by M.A.
Yes. The dropdown button in inside listview. so I’ve to store its selected value inside list not in a single value.
Comment posted by Shahzaib Ahmed
Please provide a complete list view builder code
Comment posted by M.A.
Shahzaib, I’ve updated my post. Please have a look and help me to get out of this issue if possible.
Comment posted by M.A.
Then how could I show the UI to user for the first time? I’ve to set it 1 to display the UI to user for the first time. First time it’ll 1 and later the listview will generate according to the length users selected file
Comment posted by Kaushik Chandru
fileList.length > 0? Listview.separated: Container()
Comment posted by M.A.
The whole content(dropdown, select file and comment) is in a card. That’s why I’ve to use that condition.
Comment posted by Kaushik Chandru
But in list if there is no data and you access the first element it will throw an error
Comment posted by M.A.
Ok, Kaushik. Thanks. I think I’ve to change the present UI.