How to access items from List in Flutter

October 10, 2022 No comments flutter dart list access items

Introduction

Sometimes, we may wish to access items from List in Flutter. In this tutorial, we'll look at how to do it.

How to access items from List in Flutter

To access items from a List in Flutter we can use [] operator or elementAt() method or the getRange() method in case we want to get a group of items.

Access the item at a specified index in a List using the operator []

var list = [0, 1, 2, 'three'];
print(list[2]);
print(list[3]);

The output:

2
three

Access the item at specified index in a List using elementAt() method

var list = [0, 1, 2, 'three'];
print(list.elementAt(2));

The output:

2

Get a group of items by specifying the range in the List using getRange() method

var list = [0, 1, 2, 'three'];
print(list.getRange(1, 3).toList());

The output:

[1, 2]

Modify the item at a specified index in a List using the operator []

var list = [0, 1, 2, 'three'];
list[0] = 'zero';

print(list);

The output:

[zero, 1, 2, three]

Conclusion

In this article, we presented several ways to access items from a List in Flutter/Dart.

{{ message }}

{{ 'Comments are closed.' | trans }}