Search code examples
flutterlistview

How to skip an index in an ListView Flutter


I'm trying to use a simple ListView in Flutter. The problem is that i want to use two indices (index) of my list for one card.

So if when the ListView is expanding it should always use two indices. The first Card has access to data from index 1 and 2, the second card from 3 and 4 and so on.

If anyone knows how to build something like this I'd appreciate :)


Solution

  • I see 2 ways to handle that situation...

    First

    You can generate other list where each element will contain 2 elements of the original list.

    Second

    Use isOdd or isEven to ignore some indexes.

    ListView.builder(
      itemCount: myList.length
      itemBuilder: (_, index) {
        if(index.isEven){
           return MyCustomTile(
             first: myList[index]
             second: index + 1 == myList.length ? null : myList[index+1]
           );
        }
        return Container();
      }
    )
    

    Attention to not get an index out of the bounds of your list