0

I have a listview that shows some items, inside a StatefulWidget :

  ListView.separated(
      shrinkWrap: true,
      itemBuilder: (context, index) {
          return RowItem(
              item: rowsList[index],
              index: index,
          );
      },
      separatorBuilder: (context, index) {
          return Divider(
              height: 20,
              color: Colors.transparent,
          );
      },
      itemCount: rowsList.length,
  ),

RowItem is a StatefulWidget too.

Everithing works fine when i add and remove items from rowsList, but i'm facing problems when i try to update an existing RowItem. This is my function for update:

  void updateRow(int rowIndex, MyItem item) {
    setState(() {
       rowsList[rowIndex] = item;
    });
  }

but i still see inside listview the old values for updated item. (MyItem is an object that extends Equatable).

What's wrong?

giozh
  • 9,868
  • 30
  • 102
  • 183

2 Answers2

0

try RowItem widget try this

var item;

 @override
   void initState() {
    item = widget.item;
      super.initState();
    }
@override
  void didUpdateWidget(covariant SleepDeckStepper oldWidget) {
    item = widget.item;
    super.didUpdateWidget(oldWidget);
  }

problem is item parameter is not updating for RowItem

0

Reposting my comment. You should use a Key on each RowItem.

More on them from Emily Fortuna here.

Riwen
  • 4,734
  • 2
  • 19
  • 31