How to get Element at specific Index from List in Dart/ flutter?
To get an element at specific index from a List in Dart( flutter list get element by index ), 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
.
Flutter list get element by index: Syntax
The syntax to get element at specific index index
from the List list
is
list.elementAt(index) |
List in flutter: Examples
In this example (check how to Flutter get index of item in ListView), we take a list of integers with five elements, and get elements at index 3, using List.elementAt()
method.
main.dart
void main() { var list = [ 2 , 4 , 8 , 16 , 32 ]; var element = list.elementAt( 3 ); print( 'Element : $element' ); } |
Output
Element : 16 |
RangeError Scenario
In this example (check how to Flutter get position of widget in ListView), 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
void main() { var list = [ 2 , 4 , 8 , 16 , 32 ]; var element = 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: flutter list get element by index
// 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
Flutter list get element by index: FAQs
The index() function returns the index of the element in the list, and you can use this index to get the value of the element. Note that if the element does not exist in the list, the index() function will raise a ValueError .
The first index of element in this list. Searches the list from index start to the end of the list. The first time an object o is encountered so that o == element , the index of o is returned. Returns -1 if element is not found.
. elementAt() In Dart, the . elementAt() method is used to access the element at a specific index in a list.
To check whether, and where, the element is in the list, use indexOf or lastIndexOf. To remove an element from the growable list, use remove, removeAt, removeLast, removeRange or removeWhere. To insert an element at position in the list, use insert or insertAll. List class – dart:core library – Flutter API
In Dart, the . indexOf() method returns the first occurrence of the given element in a list. If the element is not found, the method will return -1 .
Read more updates related to flutter at flutterfever.com.