Flutter list where

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

Last updated on July 27, 2021 A Goodman Loading... Loading... Post a comment
Flutter list where
report this ad

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:

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

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 Novel

Wrap 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
Advertisements

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

Advertisements
Share Tweet Telegram
Subscribe
Notify of
I allow to use my email address and send notification about new comments and replies (you can unsubscribe at any time).
Label
{} [+]
Name*
Email*
Label
{} [+]
Name*
Email*
0 Comments
Inline Feedbacks
View all comments

You May Also Like

Flutter list where
Dart: Convert Class Instances (Objects) to Maps and Vice Versa

January 7, 2022

Flutter list where
How to Flatten a Nested List in Dart

December 3, 2021

Flutter list where
Inheritance in Dart: A Quick Example

December 3, 2021

Flutter list where
Flutter & Dart: 3 Ways to Generate Random Strings

November 24, 2021

Flutter list where
Flutter & Dart: Displaying Large Numbers with Digit Grouping

November 7, 2021

Flutter list where
Using Static Methods in Dart and Flutter

October 1, 2021

Flutter list where
Prevent VS Code from Auto Formatting Flutter/Dart Code

August 24, 2021

Flutter list where
Dart: Converting a List to a Map and Vice Versa

August 19, 2021

Flutter list where
Dart: Checking whether a Map is empty

August 17, 2021

Flutter list where
Dart: Checking if a Map contains a given Key/Value

August 17, 2021

Flutter list where
Dart: How to Add new Key/Value Pairs to a Map

August 17, 2021

Flutter list where
Dart: How to Update a Map

August 17, 2021