How to get Element at specific Index from List in Dart/ flutter?

To get an element at specific index from a List in Dart, call elementAt() method on this list and pass the index as argument. elementAt() method returns the element.

If the index is out of range for the list, then elementAt() throws RangeError.

Syntax

The syntax to get element at specific index index from the List list is

list.elementAt(index)

Examples

In this example, we take a list of integers with five elements, and get elements at index 3, using List.elementAt() method.

main.dart

voidmain() {  varlist = [2, 4, 8, 16, 32];  varelement = list.elementAt(3);  print('Element :  $element');}

Output

Element :  16

RangeError Scenario

In this example, we take a list of integers with five elements, and get elements at index 7, using List.elementAt() method. Since the index is out of bounds from given list, elementAt() throws RangeError.

main.dart

voidmain() {  varlist = [2, 4, 8, 16, 32];  varelement = list.elementAt(7);  print('Element :  $element');}

Type 2: 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:

// flutterfever.com
void main() {
  final List<Map<String, dynamic>> people = [
    {"id": "c1", "name": "John Doe", "age": 40},
    {"id": "c2", "name": "flutterfever.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}

type 3: 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:

// 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', 'flutterfever.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: flutterfever.com Index: 0 Title: A Blue Novel

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More