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 its too much time consuming to create 10000 variables.
To overcome this kind of situations 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 List is called Array.
In 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 list:
- 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 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
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 hesitate 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.
Learn more about Dart and Flutter
- List in Dart
- Abstract class and Abstract Methods in Dart
- Interface in Dart
- Constructors in Dart
- Arrow Function in Dart
- User Defined Function in Dart
- Functions in Dart
- Switch case in Dart
- Conditionals in Dart
- Operators in Dart
- Keywords in Dart
- Variables in Dart
- Data Types in Dart
Top comments (0)