Dart Collections: List | List Types Fixed Lists | Growable Lists

Dart Collections: List | List Types Fixed Lists | Growable Lists

Dart, a programming language, provide a handy tool called a List for managing collections of items.

It is like dynamic array that can hold a bunch of things.

Let's dive into two types of Lists: Fixed Lists and Growable Lists.

1. Fixed Lists:

Imagine you have a box that can hold a specific number of items, let's say five. once you decide that the box can only hold five items, you can't change your mind and make it hold six or seven items later. This box is like a Fixed List in Dart.

Fixed Lists are useful when you know exactly how many items you need to store, and you're sure that number won't change while your program is running.

List<int> numbers = [1, 2, 3, 4, 5];
List<String> words = ['apple', 'banana', 'cherry'];

2. Fixed Lists: Set in Stone

A Fixed List is like a set plan. Once you create it, you decide how many items it will hold, and you can't change that number.

List<int> fixedList = List<int>.filled(3, 0);

In this example, we've created a fixed list with three slots, initially filled with zeros. You can't add or remove slots afterward.

3. Growable Lists

Fixed lists are like boxes with a set size. Once full, you can't add more or take anything out.

Growable lists are like magical boxes that can change size. You can add or remove things as you please.

You can start with a few items and add more whenever you want.

List<String> growableList = [];
growableList.add('carrot');
growableList.add('broccoli');
growableList.add('spinach');

Here, we begin with an empty list and gradually add vegetables to it. Growable Lists allow you to adjust the size as needed.

In a nutshell, Lists in Dart help you manage your data. Fixed Lists are like a set plan – once made, they stick to it. Growable Lists, on the other hand, give you flexibility – you can add or remove items whenever you please.

So, whether you're dealing with a fixed plan or embracing flexibility, Dart's Lists have got you covered!