Dart List: List Extensions | List Methods | List Properties

Vinit MepaniVinit Mepani
2 min read

Table of contents

Dart Lists: Tricks and Tools for Your Party List

In Dart, Lists are like dynamic guest lists for your programming party. Now, let's talk about some cool tricks and tools you can use to manage your guest list efficiently.

1. List Methods: Handy Helpers

Methods are like little helpers that make your tasks easier. In Dart Lists, you have methods to add, remove, and manipulate elements effortlessly.

  • Adding Guests: add()

      List<String> partyGuests = ['Alice', 'Bob', 'Charlie'];
      partyGuests.add('David');
    

    Here, we're using the add() method to invite David to the party. It's like a quick way to add a new friend to your guest list.

  • Removing Guests: remove()

      List<String> partyGuests = ['Alice', 'Bob', 'Charlie'];
      partyGuests.remove('Bob');
    

    With remove(), you can ask Bob to leave the party. It's like ushering someone out gracefully.

  • Checking Presence: contains()

      List<String> partyGuests = ['Alice', 'Bob', 'Charlie'];
      bool isBobThere = partyGuests.contains('Bob');
    

    The contains() method helps you check if Bob is still at the party. It's like scanning the room to see if your friend is around.

2. List Properties: Checking the Scene

Properties are like detectives, providing information about your list. Dart Lists come with useful properties to check their size or if they're empty.

  • Checking Size: length

      List<String> partyGuests = ['Alice', 'Bob', 'Charlie'];
      int numberOfGuests = partyGuests.length;
    

    The length property tells you how many friends are on your guest list. It's like counting heads to see how big your party is.

  • Checking if Empty: isEmpty

      List<String> partyGuests = [];
      bool isPartyEmpty = partyGuests.isEmpty;
    

    The isEmpty property helps you see if your party list is completely empty. It's like checking if your party is still in the planning phase.

3. List Extensions: Jazzing It Up

Extensions are like adding a little jazz to your list operations. They provide extra functionalities to make your coding experience more enjoyable.

  • Sorting: sort()

      List<int> numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
      numbers.sort();
    

    The sort() extension arranges your list in ascending order. It's like organizing your playlist from the oldest to the newest songs.

  • Reversing: reversed

      List<String> partyGuests = ['Alice', 'Bob', 'Charlie'];
      List<String> reversedGuests = partyGuests.reversed.toList();
    

    Using reversed, you can create a new list with your guests in reverse order. It's like changing the direction of your party vibes.

Dart Lists come with these awesome methods, properties, and extensions to make your coding party a blast. Whether you're adding, removing, or checking the scene, these tools have got you covered!

0
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!"