DEV Community

Cover image for List in Dart
Jay Tillu😎
Jay Tillu😎

Posted on • Updated on

List in Dart

  • 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:

  1. List
  2. Map
  3. Set
  4. 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:

  1. Fixed length list
  2. 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);
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Sample Code

main() {

  List<String> countries = ["India", "Nepal"];
  for (String elements in countries) {
    print(elements);
  }
}

Output
India
Nepal
Enter fullscreen mode Exit fullscreen mode

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.

For More Information Please Visit Following Links

Jai Hind, Vande Mataram 🇮🇳

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊

Top comments (0)