What is the List in Dart?

The List is the most “Common Collection” not only in the Dart, but in every Programming Language.

What do we mean by the term “Common Collection”?

The term “Common Collection” refers to another term the “Array”. The Array means an “ordered-group-of-objects”.

By the way, we use both terms equally. The List, or the Lists. Both stand for a “List-Object”.

JavaScript array literals look like Dart lists.

Here is a sample code we may consider to understand why this concept is important:

main(List<String> arguments) {
  List fruitCollection = ['Mango', 'Apple', 'Jack fruit'];
  print(fruitCollection[0]);
}
//output
Mango

Let us consider another piece of code where we have created two List Objects.

One is of the type String. And the other is the type Integer.

main(List<String> arguments) {
  List fruitCollection = ['Mango', 'Apple', 'Jack fruit'];
  var myIntegers = [1, 2, 3];
  print(myIntegers[2]);
  print(fruitCollection[0]);
}
// output
3
Mango

What is the difference between these two code snippets?

In the above code, firstly, we have explicitly mentioned the “Type” of the variable. And, that is a List.

List fruitCollection = ['Mango', 'Apple', 'Jack fruit'];

But, secondly, we have not mentioned the “Type” of the variable.

var myIntegers = [1, 2, 3];

However, Dart infers that the variable has the “Type” List.

How do you use lists in darts?

We can always make the List variable as the “Dynamic”.

void main() {
  var myIntegers = [1, 2, 3, 'non-integer object'];
  print(myIntegers[3]);
}
// output
non-integer object

It did not raise any error. But, we could have explicitly declare that the List is of “Dynamic” type.

void main() {
  List<dynamic> myIntegers = [1, 2, 3, 'non-integer object'];
  print(myIntegers[3]);
}

But, we cannot write this way. It will raise an error.

void main() {
  List<int> myIntegers = [1, 2, 3, 'non-integer object'];
  print(myIntegers[3]);
}
//output
The element type 'String' can't be assigned to the list type 'int'.

The same thing will happen if we try to add a String Type to the List of Integers.

void main() {
var myIntegers = [1, 2, 3];
myIntegers.add(' a String');
print(myIntegers[3]);
}
// output
The argument type 'String' can't be assigned to the parameter type 'int'.

We can create the “Constant List”. But we cannot change the “Constant State” of the Constant List Object.

void main() {
  var constantList = const [1, 2, 3];
  constantList[1] = 1; // This line will cause an error.
}
// output
Uncaught Error: Unsupported operation: indexed set

From the Dart 2.3 we can use the “Spread-Operator”. Other way, we can call it the “Three-Dots”.

How do I add items to my Dart list?

Now, we can easily add an item to any List.

void main() {
  var list = [1, 2, 3];
var list2 = [0, ...list];
  print(list2);
}
// output
[0, 1, 2, 3]

We can add multiple items to the List now.

Consider a List which is NULL.

What happens if we try to add any item to it?

void main() {
  var list;
var list2 = [0, ...list];
  print(list2);
}
// output
Uncaught Error: TypeError: null: type 'JSNull' is not a subtype of type 'Iterable<dynamic>'

But one “?” solves the problem.

We have learned the “NULL-SAFETY” before.

Now, we can write the above code the following way.

void main() {
  var list;
var list2 = [0, ...?list];
  print(list2);
}
// output
[0]

This is known as the “NULL-AWARE-SPREAD-OPERATOR”.

Now we can add more different “Type” of items to the List which is NULL.

void main() {
  var list;
var list2 = [0, 'A String', 1, 'Another String', ...?list];
  print(list2[0]);
  print(list2[1]);
  print(list2[2]);
  print(list2[3]);
  print(list2.length);
}
// output
0
A String
1
Another String
4

We can clearly see that it is a List of “dynamic [ ](int index)”.

The index of a List starts with 0.

However, when we measure the length, it counts from 1.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

Courses at Educative

GitHub repository

Technical blog

Twitter

Comments

6 responses to “What is the List in Dart?”

  1. […] Set is a kind of List. Although mainly unordered List. The order of the Set depends on its […]

  2. […] The list plays a very important role in any Flutter App. You may ask why? […]

  3. […] we know, at the run-time, the value of the list data type variable will […]

  4. […] We did not want to make things complicated firstly. Secondly, we tried to give an idea how we can use list in Flutter. […]

  5. […] instance, we can start with a simple list of numbers. From this list of numbers we will have to choose only odd […]

  6. […] instance, we can start with a simple list of numbers. From this list of numbers we will have to choose only odd […]

Leave a Reply