Dart List is the ordered group of objects. The dart: core library provides a List class that enables the creation and manipulation of lists. The very commonly used collection in programming is an array. Dart represents the arrays in the form of List objects. A unique number called the index identifies each element in the List. An index starts from zero and extends up to n-1 where n is a total number of items in the List.
Dart List Example
Lists can be classified into two types.
- Fixed Length List
- Growable List
Fixed Length List
A fixed-length list’s length cannot change at runtime.
The syntax for creating the fixed-length list is as given below.
var list_name = new List(initial_size)
Let’s see the following example.
// app.dart void main() { var initial_size = 5; var app_list = new List(initial_size); app_list[0] = 'Krunal'; app_list[1] = 'Ankit'; app_list[2] = 'Rushabh'; app_list[3] = 'Dhaval'; app_list[4] = 'Nehal'; print(app_list); }
Let’s see the output inside the terminal.
Growable List
The growable list’s length can change at the run-time.
The syntax for declaring and initializing a growable list is as given below.
var list_name = new List()
Let’s see the following example.
// app.dart void main() { var app_list = [19, 21, 46]; print(app_list); }
See the following output.
We can check the length of the List using the length function.
// app.dart void main() { var app_list = [19, 21, 46]; print(app_list.length); }
See the output.
Dart List Properties
We have seen one List property called length. Some other properties of List are the following.
First
This property returns the first element in the list.
// app.dart void main() { var app_list = [19, 21, 46]; print(app_list.first); }
See the output.
You can find more properties on official documentation.
Finally, Dart List Example is over.