Dart & Flutter: Get the Index of a Specific Element in a List

Last updated on July 27, 2021 A Goodman Loading... Post a comment

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 the given 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 the given conditions. If nothing found, returns -1.

The examples below will help you understand them better.

Example 1: Finding the Index of a specific Map in a List of Maps

Let’s 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<Map<String, dynamic>> _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:

Index 2: {id: c3, name: Pipi, age: 1}
Index 3: {id: c4, name: Jane Doe, age: 99}

Example 2: Finding the Index of a specific Object in a List of Objects

Let’s 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<Book> _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 Novel

Wrap Up

We’ve walked through a few examples of finding the index of a specific element in a list. If you’d like to learn more new and exciting stuff in Dart and Flutter, take a look at the following articles:

You can also check out our Flutter category page, or Dart category page for the latest tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You May Also Like