loading...
Cover image for List in Dart

List in Dart

jay_tillu profile image Jay Tillu😎 ・2 min read
  • 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);

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.

For More Information Please Visit Following Links

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊
Twitter
Facebook
Instagram
Medium
or just mail me at jayviveki13@gmail.com

Posted on by:

jay_tillu profile

Jay Tillu😎

@jay_tillu

🌏 विधायास्य धमॅस्य संरक्षणम् 🌍 ☀️ Society before Self ☀️ 🙏🏻 Humanism by Religion 🙏🏻 💻 Work for Mankind 💻 🌐 Believes in वसुधैव कुटुम्बकम् 🌐

Discussion

pic
Editor guide