Home > Enterprise >  Replacing item by id of object in Flutter from listview.builder
Replacing item by id of object in Flutter from listview.builder

Time:09-05

I am using the scrollable_positioned_list as seen here https://pub.dev/packages/scrollable_positioned_list to scroll to a specific item when a notification occurs.

By default the builder take the index to identify which index to go to, but for my purpose i need to take the ID as identifier.

Is it possible to replace this :

itemScrollController.scrollTo(
          index: 3,
          duration: Duration(milliseconds: 500),
          curve: Curves.easeInOut,
        );

but to get to the ID of the object in the list ?

CodePudding user response:

try this:

    var items = list.where((element) => element.id == 'ID');
    if (items != null && items.isNotEmpty) {
      var index = list.indexOf(items.first);
      itemScrollController.scrollTo(
          index: 3,
          duration: Duration(milliseconds: 500),
          curve: Curves.easeInOut,
        );
    }

CodePudding user response:

var item = itemList .where((ele) => ele.id == 'YOUR_ID');
    if (item != null && item.isNotEmpty) {
      var itemIndex = list.indexOf(item.first);
      itemScrollController.scrollTo(
          index: itemIndex ,
          duration: Duration(milliseconds: 500),
          curve: Curves.easeInOut,
        );
    }
  • Related