- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Lists in Dart Programming
Lists in dart are an indexable collection of objects. These can contain objects of the same type as well as objects of different data type. It is also possible that we can create a list of fixed length or a list that is growable in nature.
Lists in dart are 0 index-based.
There are mainly two types of lists in dart. These mainly are −
Fixed Length List
Growable List
We will explore both types of lists in the following article.
Fixed Length List
A Fixed length list as the name suggest cannot grow. Also, it is not allowed to manipulate the size of it to something else.
Example
Consider the example shown below −
void main(){ var fixedLengthList = List.filled(5, 3); print(fixedLengthList); }
In the above code, we are telling the compiler that we want a list that will contain int data values and also that we want to fill five indexes with a default value '3' for all.
Output
[3, 3, 3, 3, 3]
We can also calculate the length of the list using the length property of the list class in dart.
Example
Consider the example shown below −
void main(){ var fixedLengthList = List.filled(5, 3); print(fixedLengthList.length); }
Output
5
Accessing and Changing the elements of the list is also possible. We access the list elements using the bracket notation and we also change the values at a particular index with the square bracket notation.
Example
Consider the example shown below −
void main(){ var fixedLengthList = List.filled(5, 3); print(fixedLengthList[0]); // accessing the 0th index element fixedLengthList[0] = 99; // changing the element at 0 index print(fixedLengthList); }
Output
3 [99, 3, 3, 3, 3]
It should be noted that if we try to insert any element into the list or in any way try to change the length of the list then the compiler will throw an error.
Example
Consider the example shown below −
void main(){ var fixedLengthList = List.filled(5, 3); print(fixedLengthList.add(100)); print(fixedLengthList); }
Output
Error: This expression has type 'void' and can't be used. print(fixedLengthList.add(100)); ^ Error: Compilation failed.
Growable List
We can create a list that is growable by doing something like this −
Example
void main() { var growableList = [1, 2, 3]; print(growableList); }
Output
[1, 2, 3]
We can access and change the values at a particular list index without any issue.
Example
Consider the example shown below -
void main() { var growableList = [1, 2, 3]; growableList[1] = 99; print(growableList[0]); print(growableList); }
Output
1 [1, 99, 3]
It is also possible that we can add as many elements as we want, without having to run into any error as we did in a fixed-length list.
Example
Consider the example shown below −
void main() { var growableList = [1, 2, 3]; growableList.add(3); growableList.add(5); growableList.add(10); print(growableList); }
Output
[1, 2, 3, 3, 5, 10]
- Related Articles
- Comments in Dart Programming
- Constructors in Dart Programming
- Enumerations in Dart Programming
- Functions in Dart Programming
- Immutability in Dart Programming
- Inheritance in Dart Programming
- Iterables in Dart Programming
- Loops in Dart Programming
- Maps in Dart Programming
- Methods in Dart Programming
- Mixins in Dart Programming
- Queue in Dart Programming
- Runes in Dart Programming
- Set in Dart Programming
- String in Dart Programming
