Collections in Dart

 Collections in Dart


Collections are used to store multiple values in a single variable. Dart supports several types of collections, including List, Set, and Map.


void main() {

  // List

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

  print(numbers[0]); // Output: 1

  numbers.add(6);

  print(numbers); // Output: [1, 2, 3, 4, 5, 6]


  // Set

  var fruits = {'apple', 'banana', 'orange'};

  print(fruits.contains('apple')); // Output: true

  fruits.add('pear');

  print(fruits); // Output: {apple, banana, orange, pear}


  // Map

  var person = {'name': 'John Doe', 'age': 25};

  print(person['name']); // Output: John Doe

  person['height'] = 1.75;

  print(person); // Output: {name: John Doe, age: 25, height: 1.75}

}

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...