View on GitHub

reads

https://yazansneneh.github.io/reads/

Section 10: Quizzler - Modularising & Organising Flutter Code

Collections

collections in dart that i can use are lists, sets and maps

Lists:
// This list should contain only strings.
var fruits = <String>[];

fruits.add('apples');
fruits.add(5); // Error: 'int' can't be assigned to 'String'
Sets:

// add new single item ingredients.add(‘shit’);

// Adding a duplicate item has no effect. ingredients.add(‘gold’);


- remove item from set; `fruits.remove('apples')`
- check if item exist in set: `fruits.contains('gold')`.

###### Maps:
- A map, commonly known as a dictionary or hash, is an unordered collection of key-value pairs.
- Maps associate a key to some value for easy retrieval.
- Declare a map using a terse literal syntax:
```dart
// Maps often use strings as keys.
var hawaiianBeaches = {
  'Oahu': ['Waikiki', 'Kailua', 'Waimanalo'],
  'Big Island': ['Wailea Bay', 'Pololu Beach'],
  'Kauai': ['Hanalei', 'Poipu']
};

// Maps are parameterized types; you can specify what // types the key and value should be. var nobleGases = Map<int, String>();


- to add, get, and set map items use the bracket syntax.
- Use remove() to remove a key and its value from a map.
```dart
var nobleGases = {54: 'xenon'};

// Retrieve a value with a key.
assert(nobleGases[54] == 'xenon');

// Check whether a map contains a key.
assert(nobleGases.containsKey(54));

// Remove a key and its value.
nobleGases.remove(54);