Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Dart Working with Collection


Working with collections in dart involves manipulating lists, maps, and sets efficiently. Dart provides built-in classes and methods to work with these data structures effectively. Here's a brief overview:



Lists: Lists in dart are ordered collections of objects. You can add, remove, and access elements using indices. Some common operations include:
Creating a list:

List numbers = [1, 2, 3, 4, 5];

Adding elements:

numbers.add(6); // Adds 6 to the end of the list
numbers.insert(0, 0); // Inserts 0 at index 0

Accessing Elements:

int firstElement = numbers[0];


Removing Elements:

numbers.removeAt(2); // Removes the element at index 2


Iterating through a list:

for (var number in numbers) {
print(number);
}

Maps : Maps in dart are key-value pairs, where each key is unique. You can add, remove, and access elements using keys. Common operations include:
Creating a map:

Map < String, int> ages = {
'Alice': 30,
'Bob': 25,
'Charlie': 35,
};
Adding elements:
ages['David'] = 40; // Adds a new key-value pair

Accessing Elements:

int aliceAge = ages['Alice'];


Removing elements:

ages.remove('Bob');

Iterating through a map:

ages.forEach((name, age) {
print('$name is $age years old');
});

Sets: Sets in dart are unordered collections of unique elements. You can add, remove, and check for the existence of elements efficiently. Common operations include:
Creating a set:

Set< int > numbers = {1, 2, 3, 4, 5};


Adding elements:

numbers.add(6);


Removing elements:

numbers.remove(4);


Iterating through a set:

for (var number in numbers) {
print(number);
}

These are the basics of working with collections in Dart . Depending on your specific use case, Dart 's collection libraries offer additional functionality and methods to work with data effectively.






Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java