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

Updated: August 24, 2023 By: A Goodman One 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 a given element. Returns -1 if nothing is found.
  • indexWhere: Returns the first index in the list that satisfies the given conditions. If nothing is found, returns -1.
  • lastIndexOf: Returns the index of the last element in the list that equals a given element. Returns -1 if nothing is found.
  • lastIndexWhere: Returns the last index in the list that satisfies the given conditions. If nothing is 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:

// KindaCode.com
import 'package:flutter/foundation.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) {
    if (kDebugMode) {
      print("Index $index1: ${people[index1]}");
    }
  }

  // Find the last index where age > 80
  final index2 = people.lastIndexWhere((element) => element["age"] > 80);
  if (index2 != -1) {
    if (kDebugMode) {
      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 containing a given string.

The code:

// KindaCode.com
import 'package:flutter/foundation.dart';

// Define data structure for a single book
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) {
    if (kDebugMode) {
      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) {
    if (kDebugMode) {
      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
1 Comment
Inline Feedbacks
View all comments
Rustam
Rustam
2 years ago

Thanks man

Related Articles