Array is used to hold similar type of multiple values in single object. Each value is assigned with a index number position. The first element of array is located at 0 number index position and so on the next element is on 1 Index next is on Third index number. In Flutter to store multiple value we have to use Array. In array each value can be accessed via Index position. The items store in array are known as Array Elements. So in this tutorial we would learn about How to Create Use an Array in Flutter Dart Example Tutorial.
Screenshot of Array with random values :-
1. Creating Array Using Literal Constructor:
The first method of creating array is using Literal constructor [ ] . See the below code example for fully understand the coding technique.
1 2 3 4 5 6 |
void main() { var arr = ['a','b','c','d','e', 'f', 'g']; print(arr); } |
Output:
2. Creating Array Using New List Keyword:
The second easiest way to create Array items using New keyword in Dart. Using this we can easily create List in dart. List with same values respond as Array in dart.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
void main() { // Creating Array with Six items. var arr = new List(6); // Assigning values in Array on all Index. arr[0] = 'a'; arr[1] = 'b'; arr[2] = 'c'; arr[3] = 'd'; arr[4] = 'e'; arr[5] = 'f'; print(arr); } |
Output: