Dart Collections: List | List Types Fixed Lists | Growable Lists
Dart Collections: List Types
Dart, a programming language, provides a handy tool called a "List" for managing collections of items. It's like a dynamic array that can hold a bunch of things. Let's dive into two types of Lists: Fixed Lists and Growable Lists.
1. List: Your Collection Buddy
Imagine a List as a container to store various items. It could be a list of numbers, words, or anything you want. It helps you keep things organized, just like a shopping list or a to-do list.
List<int> numbers = [1, 2, 3, 4, 5];
List<String> words = ['apple', 'banana', 'cherry'];
Here, we have a list of numbers and a list of words.
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: Flexibility at Its Finest
Growable Lists are more like an expandable backpack. 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!
Subscribe to my newsletter
Read articles from Vinit Mepani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vinit Mepani
Vinit Mepani
"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation. In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology. Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities. In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"