Updated 26 December 2023
In this blog, I will explain how to create an array in Dart and the ways to initialize the array in Dart.
An array is a collection of similar data elements stored at contiguous memory locations.
It is the simplest data structure where each data element can be accessed directly by only using its index number.
The index starts from zero and extends up to n-1 where n is the total number of elements in the Array.
Dart Language is used in Flutter app development.
You may also check our Flutter app development page
The literal constructor [] can be used to generate a new array:
1 2 3 4 5 |
import 'dart:convert'; void main() { var array = ['ab','bc','cd','de','ef']; print(array); } |
1 2 3 4 5 6 7 8 9 10 11 |
import 'dart:convert'; void main() { var array = []; // assigning values to all the indices arr.add(1); arr.add(11); arr.add(111); print(array); //OutPut --> [1, 11, 111] } |
Array is of two types ->
1)Fixed Size Array
2)Variable Size Array
The elements of fixed-length arrays of elements are individually coded in the array’s natural order, 0 through n -1.
Step 1: Declaring a list is step one.
The syntax for declaring a fixed-length Array –> var arrName = new List(initial_size)
The above syntax generates a list with the requested length.
Step 2: Initialising a list is step two.
The syntax is –> arrName[index] = value;
1 2 3 4 5 6 7 8 9 |
import 'dart:convert'; void main() { var array = new List(3);// Creates an empty array of length 3 // Assigning values to all the indices array[0] = 'a'; array[1] = 'b'; array[2] = 'c'; print(array); // Output. —> [a,b,c] } |
The variable size array’s size can be changed in real-time.
Step 1: Declaring a list is step one:
var Array_name = [v1, v2, v3]
— creates a list containing the specified values
OR
var Array_name = new List()
— creates a list of size zero
Step 2: It is possible to identify the element that needs to have a value assigned by using the index or subscript. The list initialization syntax is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import 'dart:convert'; void main() { var array1 = ['a1','b1','c1','d1','e1']; print(array1); //Output array1 --> ['a1','b1','c1','d1','e1'] var array2 = new List(); array2.add(5); array2.add(6); array2.add(7); print(array2); //Output array2 --> [5, 6, 7] } |
Array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Access all element
1 2 3 4 5 6 7 8 9 10 |
import 'dart:convert'; void main() { var Array = [1, 2, 3, 4, 5, 6, 7, 8, 9]; for(int i=0; i<Array.length; i++){ print(Array[i]); } //Output 123456789 } |
Access particular element
1 2 3 4 5 6 7 8 9 |
import 'dart:convert'; void main() { var Array = [1, 2, 3, 4, 5, 6, 7, 8, 9]; Print(Array[2]); //Output 3 } |
first() | It returns the first element of the array. |
last() | It returns the last element of the array. |
sort() | sort the array element in increasing order |
isEmpty() | the checked array is Empty or not. (Returns bool value true and false) |
length() | It returns the length of the array. |
1 2 3 4 5 6 7 8 9 10 |
import 'dart:convert'; void main() { var Array = ['a', 'b', 'd', 'c', 'b', 'f']; print(Array.first); //Output --> a print(Array.last); //Output --> f print(Array.length); //Output --> 6 } |
In this article, we have explained the how to create Array in Dart.
Thanks for reading this article
For more interesting blogs check here
You can also visit the official documentation here
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.