Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – bind list of selected value in DropdownButton

Posted on November 11, 2022 By

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.

READ  [FIXED] android - How to specify the correct return type of SQLiteDatabase.query
Powered by Inline Related Posts

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.

Android Tags:android, dart, flutter, ios

Post navigation

Previous Post: [FIXED] android – How to detect when third party app’s dialog box appears in React native?
Next Post: [FIXED] android – Get the list of user SIM cards and select it – flutter

Related Posts

[FIXED] how to reformat date in android studio Android
[FIXED] java – How can I fetch data from my Database using Room when the input query is only part of that data? Android
[FIXED] android – How do I chain together layout change animations with the LayoutTransition class or otherwise? Android
[FIXED] android – Can I fetch the data being given to an app by its host and store it locally? Android
[FIXED] android – ClassNotFoundException when using androidx.fragment.app.FragmentContainerView Android
[FIXED] java – Functioning App crashing on installing on Android device Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Which is stronger 19 gauge or 23 gauge?
  • Does BMW still use GM transmissions?
  • Is primary or secondary market research better?
  • What does it mean when ADT says low battery?
  • Can a baby sleep on a regular mattress?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme