2

I have a Map of location data like this :

{
  'country': 'Japan',
  'city': 'Tokyo',
  'Latitude': 35.6762,
  'Longitude': 139.6503,
  'utcOffset': 9
}

witch is the right solution for storing this data and why ?

1) list of maps :

List<Map<String, dynamic>> locations = [
 {
      'country': 'Japan',
      'city': 'Tokyo',
      'Latitude': 35.6762,
      'Longitude': 139.6503,
      'utcOffset': 9
    }
];

or multi level data object

var locations = {
{
      'country': 'Egypt',
      'city': 'Cairo',
      'Latitude': 30.033333,
      'Longitude': 31.233334,
      'utcOffset': 2
    },
    {
      'country': 'Thailand',
      'city': 'Bangkok',
      'Latitude': 13.7563,
      'Longitude': 100.5018,
      'utcOffset': 7
    },
};

And how to access the data in the 2 cases?

Blasanka
  • 21,001
  • 12
  • 102
  • 104
Ahmed Osama
  • 672
  • 1
  • 8
  • 15

3 Answers3

5

I usually use Map when I want to have key-value pairs because I can directly get the value by key. As a example if you have Map of employes and if you keep the employee id as a key, you can easily access it. Specially for search, or for drop down this is elegant.

Most of the time if I can avoid Map and I will use List because it is very easy for me to handle large amount of data(In flutter I can throw a list to a ListView easily).

But both have there own greatness for depending on the scenario. And note that Map cannot have duplicate key, where List doesnt have like that limitation(Ex: Map can use to avoid duplicate).

  var locations = {
    {
      'country': 'Egypt',
      'city': 'Cairo',
      'Latitude': 30.033333,
      'Longitude': 31.233334,
      'utcOffset': 2
    },
    {
      'country': 'Thailand',
      'city': 'Bangkok',
      'Latitude': 13.7563,
      'Longitude': 100.5018,
      'utcOffset': 7
    },
  };

  for (var element in locations) {
    print(element["country"]);
  }

  // or

  locations.forEach((element) => print(element["country"]));

Here what doc contains about this:

Map<K, V> class

A collection of key/value pairs, from which you retrieve a value using its associated key.

There is a finite number of keys in the map, and each key has exactly one value associated with it.

Maps, and their keys and values, can be iterated. The order of iteration is defined by the individual type of map.

List<E> class

An indexable collection of objects with a length.

Subclasses of this class implement different kinds of lists. The most common kinds of lists are:

Fixed-length list. An error occurs when attempting to use operations that can change the length of the list.

Growable list. Full implementation of the API defined in this class.

The default growable list, as returned by new List() or [], keeps an internal buffer, and grows that buffer when necessary. This guarantees that a sequence of add operations will each execute in amortized constant time. Setting the length directly may take time proportional to the new length, and may change the internal capacity so that a following add operation will need to immediately increase the buffer capacity. Other list implementations may have different performance behavior.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
3

You can use both but the first one is preferred as its easier to loop through the items and get the data out. Not that you can't loop through the second data set, just that its more tedious to loop an array.

Given that your array of objects look like this:

List<Map<String, dynamic>> locations = [
 {
      'country': 'Japan',
      'city': 'Tokyo',
      'Latitude': 35.6762,
      'Longitude': 139.6503,
      'utcOffset': 9
    }
];

Suppose you want to take out all the country names and store them in a separate array, It would be really easy and the code should look as follows:

List<String> countryNames = [];
locations.forEach((location) => countryNames.add(location['country]));
Yudhishthir Singh
  • 2,941
  • 2
  • 23
  • 42
1

You can get data dynamically from a Map creating a this function getData

  dynamic getData(Map data, List<String> way) {
    dynamic dataTemp = data;
    if (way.length > 0) {
      for (int x=0; x < way.length; x++) {
        dataTemp = dataTemp[way[x]];
      }
    }
    return dataTemp;
  }

    List<Map<String, dynamic>> locations = [
 {
      'country': 'Japan',
      'city': 'Tokyo',
      'Latitude': 35.6762,
      'Longitude': 139.6503,
      'utcOffset': 9,
      'example' : {
        'data'   : "text",
        'number' : 20,
        'boolean': false
       }
    }
];


 getData(locations[0],["example","number"]);

output: 20