Working with List in Dart Programming

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:

  1. Growable List: A growable list allows you to add or remove elements dynamically.
  2. Fixed-Length List: You must specify the size at creation, and it cannot grow or shrink.
  3. Unmodifiable List: A list that cannot be changed after it's created (read-only).
  4. Constant List : A compile-time constant list — similar to unmodifiable but defined with const.
  5. Typed List: Lists can be strongly typed to a specific type.
  6. 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
Using for 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 Set
Set<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

Kailash Chandra Behera

An IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

نموذج الاتصال