Map

  1. Definition
  2. A collection of key/value pairs, from which you retrieve a value using its associated key.
    Keys and values in a map may be of any type.
    Each key can only occurs once and it is used to accessed corresponding Map value.


  3. Syntax

  4. var <map_name> = new Map();
    <map_name>[key]=value;

    OR

    var <map_name> = {key1:value1,key2:value2}


  5. Example

  6. var student = new Map();
    student["name"] = "Adem";
    student["age"] = 18;

    OR

    var student = {
       "name": "Paul Smith",
       "age": 22,
       "id": 980908232
    };


  7. Properties

  8. Property Description
    entries The map entries of this.
    hashCode The hash code for this object.
    isEmpty Returns true if there is no key/value pair in the map.
    isNotEmpty Returns true if there is at least one key/value pair in the map.
    keys The keys of this.
    length The number of key/value pairs in the map.
    runtimeType A representation of the runtime type of the object.
    values The values of this. [...]

  9. Methods

  10. Method Description
    addAll(Map other) Adds all key/value pairs of other to this map.
    addEntries(Iterable> newEntries) Adds all key/value pairs of newEntries to this map.
    cast() Provides a view of this map as having RK keys and RV instances, if necessary.
    clear() Removes all pairs from the map.
    containsKey(Object? key) Returns true if this map contains the given key.
    containsValue(Object? value) Returns true if this map contains the given value.
    forEach(void f(K key, V value)) Applies f to each key/value pair of the map.
    map(MapEntry f(K key, V value)) Returns a new map where all entries of this map are transformed by the given f function.
    noSuchMethod(Invocation invocation) Invoked when a non-existent method or property is accessed.
    putIfAbsent(K key, V ifAbsent()) Look up the value of key, or add a new value if it isn't there.
    remove(Object? key) Removes key and its associated value, if present, from the map.
    removeWhere(bool predicate(K key, V value)) Removes all entries of this map that satisfy the given predicate.
    toString() Returns a string representation of this object.
    update(K key, V update(V value), {V ifAbsent()}) Updates the value for the provided key.
    updateAll(V update(K key, V value)) Updates all values.

  11. Example

  12. enum transmission { manual, automatic }

     

    void main() {

      var car = {

        "model""R8",

        "Horsepower"602,

        "Energy""Gasoline",

        "transmission": transmission.automatic

      };

      print(car.length);

      print(car.keys);

      print(car.values);

      car.addAll({"airbags"true});

      print(car);

      car["model"] = "Audi R8";

      print(car);

      car.update("Horsepower", (value) => 650);

      print(car);

      car.remove("Energy");

      print(car);

      car.updateAll((key, value) => "unkown");

      print(car);

      car.clear();

      print(car);

    }




    4

    (model, Horsepower, Energy, transmission)

    (R8, 602, Gasoline, transmission.automatic)

    {model: R8, Horsepower: 602, Energy: Gasoline, transmission: transmission.automatic, airbags: true}

    {model: Audi R8, Horsepower: 602, Energy: Gasoline, transmission: transmission.automatic, airbags: true}

    {model: Audi R8, Horsepower: 650, Energy: Gasoline, transmission: transmission.automatic, airbags: true}

    {model: Audi R8, Horsepower: 650, transmission: transmission.automatic, airbags: true}

    {model: unkown, Horsepower: unkown, transmission: unkown, airbags: unkown}

    {}