Home > Enterprise >  How to get the index of an item inside a list on flutter
How to get the index of an item inside a list on flutter

Time:01-18

Im trying to get the index of and item inside a list which is inside a Column() in flutter, how do I do that? I've tried making a var called index and set it to 0 and have it increment by each item that is build but it doesn't seem to work out and I don't know why.

And it can't be made with ListView.builder() because of some other reasons

My index var:

int index = 0;

My code:

                            markerPointers: _sortedList
                                .map(
                                  (data) => LinearWidgetPointer(
                                    value: data.points,
                                    position: LinearElementPosition.cross,
                                    animationDuration: 2000,
                                    offset: 500,
                                    child: UserCardWidget(
                                      data: data,
                                      index: index   1,
                                      isExpandedMethod: _isExpanded,
                                    ),
                                  ),
                                )
                                .toList(),

CodePudding user response:

user .asMap() it will convert your list to a map with the keys equal to the index.

markerPointers: category._sortedList
                    .asMap()
                    .map(
                      (i, element) {
                        return MapEntry(
                          i,
                          LinearWidgetPointer(
                                value: data.points,
                                position: LinearElementPosition.cross,
                                animationDuration: 2000,
                                offset: 500,
                                child: UserCardWidget(
                                  data: data,
                                  index: i,
                                  isExpandedMethod: _isExpanded,
                                ),
                              ),
                        );
                      },
                    )
                    .values
                    .toList(),
  •  Tags:  
  • Related