Dart .indexOf()
Published Feb 22, 2024
In Dart, the .indexOf() method returns the first occurrence of the given element in a list. If the element is not found, the method will return -1.
Syntax
list_name.indexOf(element);
list_name: The name of the list where the element is searched.element: The value whose index is searched for in the list.
Example
Here is an example of the .indexOf() method:
void main() {List<String> colors = ['blue', 'yellow', 'red', 'green', 'orange', 'pink'];int index = colors.indexOf('red');print(index);}
The output for the above example is as follows:
2
Learn Dart on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours