Lists in Dart are equivalent to arrays in other programming languages but with more built-in capabilities.In this post, we’ll explore everything you need to know about working with Lists in Dart, from basic usage to advanced operations.
Working with List in Dart Programming
Getting Started
Dart is an object-oriented, class-based programming language used widely for building mobile, web, and server applications, especially with Flutter. One of the most commonly used data structures in Dart is the List. Dart List in Dart are ordered collections of objects and are equivalent to arrays in other programming languages.
What is a Dart List?
A Dart List is an ordered collection of elements. It can contain elements of any data type and supports both fixed-length and growable versions.
Dart List Example void main() {
List<int> dartlist= [1, 2, 3, 4, 5];
print(dartlist); // Output: [1, 2, 3, 4, 5]
}
Type of Dart List
Dart provides several types of lists, each suited for different use cases. Here's an overview of the common types of lists in Dart:
- Growable List: A growable list allows you to add or remove elements dynamically.
- Fixed-Length List: You must specify the size at creation, and it cannot grow or shrink.
- Unmodifiable List: A list that cannot be changed after it's created (read-only).
- Constant List : A compile-time constant list — similar to unmodifiable but defined with const.
- Typed List: Lists can be strongly typed to a specific type.
- Spread and Collection-if Lists: You can use spread (...) and if conditions when creating lists.
Common Operations on Dart List
In Dart, List is a commonly used collection to store ordered data. Here are some common operations you can perform on a List:
Creating a List
var numbers = [1, 2, 3];
List<String> names = ['Alice', 'Bob'];
Adding Elements
numbers .add(4); // Add one element
numbers .addAll([5, 6]); // Add multiple elements
numbers .insert(0, 0); // Insert at index
numbers .insertAll(1, [100, 200]); // Insert multiple at index
Updating Elements
numbers[1] = 10; // Update value
Removing Elements
numbers.remove(2); // Remove value
numbers.removeAt(0); // Remove by index
numbers.removeLast(); // Remove last item
numbers.removeWhere((item) => item > 5); // Conditional remove
Accessing Elements
print(numbers[0]); // Access by index
numbers[1] = 10; // Update value
Iterating a Dart List
Usingfor
Loop
for (var num in numbers) {
print(num);
}
Using for-in
Loop
for (var num in numbers) {
print(num);
}
Using forEach
Loop
numbers.forEach((num) => print(num));
Checking List Properties
numbers.isEmpty;
numbers.isNotEmpty;
numbers.length;
numbers.contains(3);
Sorting and Reversing
numbers.sort(); // Sort ascending
numbers.sort((a, b) => b.compareTo(a)); // Sort descending
var reversed = numbers.reversed.toList(); // Reverse list
Searching / Filtering
numbers.firstWhere((item) => item > 5, orElse: () => -1);
numbers.where((item) => item % 2 == 0).toList();
Mapping and Transforming
var doubled = numbers.map((item) => item * 2).toList();
Sublist and Range
var sub = numbers.sublist(1, 3); // From index 1 to 2
Converting Other Types to List
From SetSet<int> numberSet = {1, 2, 3};
List<int> numberList = numberSet.toList();
From String
String csv = "apple,banana,grape";
List<String> items = csv.split(',');
Summary
Lists are a fundamental part of Dart programming. Whether you're building a simple app or a complex Flutter UI, understanding how to work with Lists efficiently is crucial. Dart’s List API is rich and supports a variety of operations, making it a powerful tool for managing collections.
Thanks