.indexWhere()
Anonymous contributor
Published Mar 5, 2024
Contribute to Docs
In Dart, the .indexWhere()
method is used to find the index of the first element in a list that fulfills a given condition. If the operation is successful, it returns the index. Otherwise, it returns -1
.
Syntax
int indexWhere(bool func(S demo))
func
: A function that provides a condition for the.indexWhere()
method to check.S
: The type of element that is passed to the function.demo
: The element that is passed to the function.
Example
In this example, the .indexWhere()
method is used to find the index of the first negative integer in a list:
void main() {List<int> numbers = [8, 5, 2, -3, 6];int index = numbers.indexWhere((number) => number < 0);if (index != -1) {print('Index of the first negative integer: $index');} else {print('No negative integers found.');}}
The above example returns the following output:
Index of the first negative integer: 3
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.