Dart Lists
Dart Lists allow you to work with a collection of objects. They are similar to arrays in other programming languages but can be growable or fixed-length.
Dart Lists
Dart Lists are collections of objects accessible by index. They can hold objects of different types and have methods to manipulate elements.
Create a List
To create a list we can use square brackets and commas to separate the elements. The general syntax to create a list in Dart is as follows:
data_type list_name = [elem1, elem2, … , elemN];
For example
var numbers = [10, 20, 30, 40, 50];
Access an element
To access an element in the list, we can use the index :
For example, to access and print the first element in the list, we can use the
the following code:
print(numbers[0]);
Create a growable List
To create a growable list, we can use the list List.generate() constructor.
For Example:
var growableList = List.generate(6, (i) => (i + 1) * 3);
Add an element
To add an element to the list, we can use the add() method.
For example, to an element to the list,
growableList.add(21);
Remove an element
To remove an element from the list, we can use the following methods
- remove()
- removeAt()
- removeLast()
- removeWhere()
For example:
To remove element 18 from the list:
growableList.remove(18);
To remove the first element from the list:
growableList.removeAt(0);
—
Dart Tutorials
Dart tutorial on this website can be found at:
https://www.testingdocs.com/dart-tutorials-for-beginners/
More information on Dart: