Anget
Anget

Reputation: 61

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

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 ?

Upvotes: 1

Views: 458

Answers (2)

Thusitha Deepal
Thusitha Deepal

Reputation: 1546

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,
        );
    }

Upvotes: 0

eamirho3ein
eamirho3ein

Reputation: 17950

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,
        );
    }

Upvotes: 0

Related Questions