Dart & Flutter: Get the Index of a Specific Element in a List
In Dart, the List class has 4 methods that can help you find the index of a specific element in a list:
- indexOf: Returns the first index of the first element in the list that equals to a given element. Returns -1 if nothing found.
- indexWhere: Returns the first index in the list that satisfies thegiven conditions. If nothing found, returns -1.
- lastIndexOf: Returns the index of the last element in the list that equals to a given element. Returns -1 if nothing found.
- lastIndexWhere: Returns the last index in the list that satisfies thegiven conditions. If nothing found, returns -1.
The examples below will help you understand them better.Advertisements
Example 1: Finding the Index of a specific Map in a List of Maps
Lets say we have a list of people with information including id, name, and age. The first task is to find the index of the person whose id equals a given id [because the ids are unique we can have at most 1 result]. The second task is to find the index of the last person whose age is greater than 80.
The code:
// main.dart void main[] { final List _people = [ {"id": "c1", "name": "John Doe", "age": 40}, {"id": "c2", "name": "Kindacode.com", "age": 3}, {"id": "c3", "name": "Pipi", "age": 1}, {"id": "c4", "name": "Jane Doe", "age": 99}, ]; // Find index of the person whose id = c3 final index1 = _people.indexWhere[[element] => element["id"] == "c3"]; if [index1 != -1] { print["Index $index1: ${_people[index1]}"]; } // Find the last index where age > 80 final index2 = _people.lastIndexWhere[[element] => element["age"] > 80]; if [index2 != -1] { print["Index $index2: ${_people[index2]}"]; } }Output:
Example 2: Finding the Index of a specific Object in a List of Objects
AdvertisementsLets say we have a list of given books. The first job is to find the index of the book whose id equals a given id. The second job is to find the index of the first book in the list that has the title contains a given string.
The code:
// main.dart class Book { String id; String title; double price; Book[this.id, this.title, this.price]; } void main[] { final List _books = [ Book['b1', 'A Blue Novel', 0.99], Book['b2', 'A Green Novel', 1.99], Book['b3', 'A Cooking Hanbook', 2.99], Book['b4', 'Kindacode.com', 0.00] ]; // Find the index of the book whose id is 'b4' final int index1 = _books.indexWhere[[[book] => book.id == 'b4']]; if [index1 != -1] { print['Index: $index1']; print['Title: ${_books[index1].title}']; } // Find the index of the fist book whose title contains 'Novel' final int index2 = _books.indexWhere[[book] => book.title.contains['Novel']]; if [index2 != -1] { print['Index: $index2']; print['Title: ${_books[index2].title}']; } }Output:
Index: 3 Title: Kindacode.com Index: 0 Title: A Blue NovelWrap Up
Weve walked through a few examples of finding the index of a specific element in a list. If youd like to learn more new and exciting stuff in Dart and Flutter, take a look at the following articles:
- Dart: Find List Elements that Satisfy Conditions
- Dart regex to validate US/CA phone numbers
- Dart: Capitalize the First Letter of Each Word in a String
- Dart & Flutter: 2 Ways to Count Words in a String
- 4 ways to convert Double to Int in Flutter & Dart
You can also check out our Flutter category page, or Dart category page for the latest tutorials and examples.
You May Also Like
January 7, 2022
December 3, 2021
December 3, 2021
November 24, 2021
November 7, 2021
October 1, 2021
August 24, 2021
August 19, 2021
August 17, 2021
August 17, 2021
August 17, 2021
August 17, 2021