Lists In Dart - Learn Dart Programming 4
Table of Contents
Introduction
In this tutorial, we will explore how to work with lists in Dart, a modern programming language used for building mobile, desktop, and web applications. Lists in Dart are essential data structures that allow you to store collections of items. We will cover creating lists, modifying them, and managing items at specific index locations. This guide will give you a solid understanding of how to effectively use lists in your Dart projects.
Step 1: Create a List
To start working with lists in Dart, you first need to create one. You can create a list using either a literal or the List constructor.
- Using a List literal:
var numbers = [1, 2, 3, 4, 5];
- Using the List constructor:
var numbers = List<int>.filled(5, 0); // Creates a list of 5 integers initialized to 0
Step 2: Change an Item in a List
You can modify an existing item in a list by accessing it through its index.
- To change an item:
numbers[0] = 10; // Changes the first item to 10
Step 3: Create an Empty List
Creating an empty list is straightforward and is often used when you plan to add items later.
- To create an empty list:
var emptyList = [];
Step 4: Add Item to Empty List
You can add items to an empty list using the .add()
method.
- To add a single item:
emptyList.add(1); // Adds 1 to the empty list
Step 5: Add Multiple Items to List
To add multiple items at once, use the .addAll()
method.
- To add multiple items:
emptyList.addAll([2, 3, 4]); // Adds 2, 3, and 4 to the list
Step 6: Insert Item at Specific Location
If you need to add an item at a specific index, use the .insert()
method.
- To insert an item:
emptyList.insert(1, 5); // Inserts 5 at index 1
Step 7: Insert Multiple Items at Specific Locations
To insert multiple items at specific locations, you can use a combination of loops or utilize custom functions for better management.
- To insert multiple items:
emptyList.insertAll(2, [6, 7]); // Inserts 6 and 7 starting at index 2
Step 8: Create List with Multiple Data Types
Dart allows you to create lists that can hold different data types.
- To create a mixed-type list:
var mixedList = [1, 'two', 3.0, true]; // Contains an int, a string, a double, and a boolean
Step 9: Remove Item from List
To remove an item from a list, use the .remove()
method.
- To remove a specific item:
mixedList.remove('two'); // Removes the string 'two' from the list
Step 10: Remove Item at Specific Index Location
If you need to remove an item based on its index, use the .removeAt()
method.
- To remove an item by index:
mixedList.removeAt(0); // Removes the item at index 0
Conclusion
In this tutorial, you learned how to create and manipulate lists in Dart, including how to add, insert, and remove items. Mastering lists is crucial for managing collections of data efficiently in your Dart applications. As you continue to explore Dart, consider practicing these list operations in various projects to strengthen your programming skills. Happy coding!