List in Dart
In Dart, Collections are used to store data. The list is a collection of data having the same data type. In other languages, the List is called Array.
Consider a situation where we need to store five String values. If we use programming’s simple variable and data type concepts, then we need five variables of String data type.
It seems simple because we had to store just five String values. Now let’s assume we have to store 10000 String values. Now it's too time-consuming to create 10000 variables.
To overcome this kind of situation we have a concept called Collections.
Collections are used to store data.
In Dart, collections can be classified into four basic categories:
List
Map
Set
Queue
But the list and map are mostly used. Set and queue are used in some special cases.
List
The list is a collection of data having the same data type. In other languages, the List is called Array.
In a list, data is stored in an ordered way.
Every individual element of the list can be accessed by its index number. The index number always starts from 0.
There are two types of lists:
Fixed length list
Growable list
Fixed Length List
In the fixed length list once the length of the list is defined it cannot be changed during run time.
Syntax
List <data type> list_name = List (length of list);
Sample Code
main() {
List<int> marks = List(5);
marks[0] = 25; // Inserting values to list
marks[1] = 35;
marks[2] = 65;
marks[3] = 75;
marks[4] = 63;
for (int elements in marks) {
print(elements);
}
}
Output
25
35
65
75
63
Growable List
The length of the Growable list is dynamic. It can be changed during run time.
Syntax
List <data type> list_name = List (); // Way 1
List <data type> list_name = [element1, element2]; // Way 2
Sample Code
main() {
List<String> countries = ["India", "Nepal"];
for (String elements in countries) {
print(elements);
}
}
Output
India
Nepal
Conclusion
This article discusses collections in Dart, which are used to store data efficiently. Dart has four basic types of collections: List, Map, Set, and Queue, with List and Map being the most commonly used ones.
List: A List in Dart is similar to an array in other languages. It is a collection of data elements of the same data type, stored in an ordered manner. Each element in a List can be accessed using an index, starting from 0. There are two types of Lists in Dart:
Fixed Length List: In a fixed length List, the length of the List is defined at the time of creation and cannot be changed during runtime.
Growable List: A Growable List, on the other hand, has a dynamic length that can be changed during runtime. You can add or remove elements as needed.
Collections are especially useful when you need to store a large number of values without creating individual variables for each one, making your code more efficient and manageable.
So, guys, That’s it for a list. As I always say practice it, understand it conceptually. Till then Keep Loving, Keep Coding. And just like always I’ll surely catch you up in the next article. 😊
Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitant that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.