Yonkee
Yonkee

Reputation: 1883

Listview builder not updating values in list

I have a positioned ListViewBuilder inside a Stack attempting to show a dynamic drop down across multiple tiles with different drop down values. This seems to work fine, except for the fact that the values don't seem to clear correctly when the list is updated with new values and refreshed on SetState.

ListView builder code

Positioned(
              top: 100,
              left: 100,
              width: 90,
              height: MediaQuery.of(context).size.height,
              child: ListView.builder(
                shrinkWrap: true,
                scrollDirection: Axis.vertical,
                itemCount: dropDownListValues.length,
                itemBuilder: (context, index) {
                  return Tile(
                    tileId: dropDownListValues[index],
                    isHeaderTile: false,
                    uuid: Uuid().v1(),
                  );
                },
              ),
            ),

Clear and update list based on which button is pressed.

  void _toggleDropDown(List valueList) {
    if (dropDownListValues.isEmpty) {
      setState(() {
        dropDownListValues = valueList;
      });
    } else {
      setState(() {
        dropDownListValues.clear();
      });
    }
  }

What I end up getting is the list extends based on the number of items in the drop down, but the values from the previous list carry across from the last list..

Example.

Drop Down list values

Dropdown list 1 = ['one', 'two']
Dropdown list 2 = ['two', 'three', 'four']

What I am seeing is

Dropdown list 1 = ['one', 'two']
Dropdown list 2 = ['one', 'two', 'four']

The if I click on list two drop down first I get the following

Dropdown list 1 = ['two', 'three']
Dropdown list 2 = ['two', 'three', 'four']

Im stumped and have spend hours on this, any idea what I could be doing wrong to cause this refresh issue?

Thanks in advance.

Upvotes: 0

Views: 656

Answers (1)

Michel Feinstein
Michel Feinstein

Reputation: 14266

Try using keys so the Flutter Framework can identify changes to your list.

Upvotes: 1

Related Questions