Collections

Maps

// a map is an object that associates keys and values
var person = Map<String, String>();
// To initialize the map, do this:
person['firstName'] = 'Nicola';
person['lastName'] = 'Tesla';
print(person); //Print: {firstName: Nicola, lastName: Tesla}
print(person['lastName']); //Print: Tesla
var nobleGases = {
  // Key: Value
  2: 'helium',
  10: 'neon',
  18: 'argon',
};

Sets

// A set in Dart is an unordered collection of unique items.
var halogens = {'fluorine', 'chlorine', 'bromine', 'iodine', 'astatine'};
// to create an empty set
var names = <String>{};
Set<String> names = {}; // This works, too.
//var names = {}; // Creates a map, not a set.

Lists

// ordered group of objects
var list = [1, 2, 3];
print(list.length); //Print: 3
print(list[1]); //Print: 2
// other ways of list declaration and initializations
List<String> cities = <String>["New York", "Mumbai", "Tokyo"];
// To create a list that’s a compile-time constant
const constantCities = const ["New York", "Mumbai", "Tokyo"];
Comments