Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 489 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get iteration index from List.map()

#11
Can also try this:


list.asMap().keys.toList().map((index) {
// var item = list[index];
// index is the index of each element.
return youFunction(index);
});



I also write other approaches as well in my blog here.

[To see links please register here]

Reply

#12
its pretty simple and straight forward in dart.

myList.map((item) =>
print(myList.indexOf(item));//here I printing index
).toList()
Reply

#13
You can simply use the `mapIndexed` method:


```
userBoard.mapIndexed(
(int index, element) => Container( ... ))
.toList();
```

This is one of the many extensions from the FIC package:

[To see links please register here]


---

If you don't want to add a package to your project you can just copy the extension itself:


```
extension FicListExtension<T> on List<T> {

/// Maps each element of the list.
/// The [map] function gets both the original [item] and its [index].
Iterable<E> mapIndexed<E>(E Function(int index, T item) map) sync* {
for (var index = 0; index < length; index++) {
yield map(index, this[index]);
}
}
}
```

_Note: I'm one of the authors of that package._

---

**Update:** I have now removed a few methods, like `Iterable.mapIndexed()` from the [FIC package][1], because Google added a similar method to their [collection package][2], and I want both packages to be compatible.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

:collection/collection.dart
Reply

#14
Dart has released the [`collection`](

[To see links please register here]

) package that comes with a [`mapIndexed`](

[To see links please register here]

) extension to all Iterables.

```dart
import 'package:collection/collection.dart';

void main() {
final fruitList = ['apple', 'orange', 'mango'];
final withIndices = fruitList.mapIndexed((index, fruit) => "$index - $fruit");
print(withIndices);
}
```
([DartPad](

[To see links please register here]

))

The package comes with all sorts of handy extensions and useful tools to work with collections.
Reply

#15
You can use asMap() and entries.map() to get the index.

```dart
list.asMap().entries.map((e) {
var index = e.key;
var value = e.value;
// ...
}
```
Reply

#16
The current version of built-in [collection package][1] now contains the method `mapIndexed` and comprehensive set of other `List`/`Iterable` [extension methods][2].

```dart
import 'package:collection/collection.dart';
```


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#17
```
Map map = {
'1st':'first',
'2nd':'second'}; //any data

List mapAsList = map.keys.toList(); //convert map to list to get iteration index

print( map[mapAsList[0]] ); //first
print( map[mapAsList[1]] ); //second
```
Reply

#18
Try this: [source][1]
--------
(Key in map) same (index in list)

myList.asMap().entries.map((entry) {
int idx = entry.key;
String val = entry.value;

return something;
}


[1]:

[To see links please register here]

Reply

#19
To get access to index, you need to convert your list to a map using the [asMap][1] operator.

**Example**

final fruitList = ['apple', 'orange', 'mango'];
final fruitMap = fruitList.asMap(); // {0: 'apple', 1: 'orange', 2: 'mango'}

// To access 'orange' use the index 1.
final myFruit = fruitMap[1] // 'orange'

// To convert back to list
final fruitListAgain = fruitMap.values.toList();

**Your Code**

userBoard.asMap().map((i, element) => MapEntry(i, Stack(
GestureDetector(onTap: () {
setState(() {
// print("element=${element.toString()}");
// print("element=${userBoard[i].toString()}");
});
}),
))).values.toList();

**References to other answers**
- I like [this answer][2] better. Please take a look.
- If you want in multiple places try extending [like this][3].
- Alternatively, you could also try the [dart collection approach][4].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

Reply

#20
You can do it by defining a variable named index:

```js
Widget listWidget() {

List < String > myList = ['A', 'B', 'C'];

int index = -1;

return Column(
children: myList.map < Widget > ((element) {
index++;
return Container(
Text("$index : $element")
);
}).toList()
);
}
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through