🧺 What is a List in Dart?

Lists in Dart

Let’s open up that toy box and look even closer at the special things we can do with a Dart List.

1. The Super Toy Box (Creating a List)

A Dart List is like a special container that uses square brackets [] to hold all the items, with a comma , separating each item.

What we want to storeDart Code
A list of numbers (like toy scores)var scores = [10, 20, 30];
A list of names (like friends)var friends = ['Mia', 'Sam', 'Tom'];
A box with different types of thingsvar mixedBox = [1, 'apple', true];

2. The Finger Pointer (Accessing Items)

Remember how we use the index to find a specific toy? This is a super important trick!

  • The counting starts at zero (0)!
Item PositionIndex NumberItem
First Item0friends[0] is ‘Mia’
Second Item1friends[1] is ‘Sam’
Third Item2friends[2] is ‘Tom’

The Trick: You use the list name, and then the index number in square brackets.

var friends = ['Mia', 'Sam', 'Tom'];
print(friends[0]); // Output: Mia

3. Making Changes to the Box (Modifying a List)

Your toy box is not sealed shut! You can always make changes.

What you want to doDart Code
Add a new item to the endfriends.add('Zoe');
Take out an item (by its value)friends.remove('Sam');
Replace an item (using the index)friends[0] = 'Liam'; (Replaces ‘Mia’ with ‘Liam’)
Count how many items are in the boxprint(friends.length);
Lists in Dart

4. A Special Kind of Box (Fixed vs. Growable)

Most of the Lists you will use are Growable. This means you can keep adding or removing items whenever you want.

But sometimes, you might need a special box that cannot change its size once you make it. That’s a Fixed-Length List. You tell Dart, “This box will only ever hold 5 toys, and no more!”

But for almost all your coding, you’ll use the easy, Growable List (like the ones we made above with var friends = [...]).

In short: A Dart List is a simple, organized way to store a group of things where the order matters, and you can easily look up, add, or change any of the items using a starting-from-zero number called an index.

Read More: Flutter Version: Latest Version, Prompt for Check Version and Update Note

Summary

  • A List is like a box or basket.
  • You can put things in, take things out, and look at them.
  • Dart uses Lists to keep things in order.

Leave a Reply

Your email address will not be published. Required fields are marked *