Home > Mobile >  Dart/Flutter: How can i calculate values of multiple maps which contains same key?
Dart/Flutter: How can i calculate values of multiple maps which contains same key?

Time:12-16

I am new to Dart and i got stuck at this point for days.

I have multiple maps and these maps follow the same pattern (date:value) but their lengths are different. And these maps are increased dynamically.

For example:

Map1:{01.01.2021:2,02.01.2021:5,03.01.2021:3}, 
Map2:{02.01.2021:10,03.01.2021:4,04.01.2021:8}, 
Map3...
Map4...
...

I want to combine these maps and sum the values that contains the same key and store it in an another map. For the different keys, there will be no calculation and store as it is.

Result for Map1 & Map2:

Combined Map{01.01.2021:2,02.01.2021:(5 3),03.01.2021:(3 4),04.01.2021:8}

How can i perform such an operation considering that these maps are iterable inside a class or inside an another list.

Thank you in advance.

CodePudding user response:

Here you go! Paste this on DartPad

This will work with any number of maps inside data map

Any questions fell free to ask me in the comments.

void main() {
  
  final data = {
    'Map1': {'01.01.2021':2,'02.01.2021':5,'03.01.2021':3}, 
    'Map2': {'02.01.2021':10,'03.01.2021':4,'04.01.2021':8}, 
  };

  final finalData = {};
  
  for(final key in data.keys) {
    for(final date in data[key]!.keys) {
      final initialValue = finalData[date];
      
      if(initialValue == null) {
        finalData[date] = data[key]![date];
      } else {
        finalData[date] = initialValue   data[key]![date];
      }
    }
  }
  // {01.01.2021: 2, 02.01.2021: 15, 03.01.2021: 7, 04.01.2021: 8}
  print(finalData);
}

The inner loop can be written more succinctly using the tertiary operator

for (final date in data[key]!.keys) {
  final initialValue = finalData[date];
  finalData[date] = initialValue == null
      ? data[key]![date]
      : initialValue   data[key]![date];
}

or the null aware operator (??)

for (final date in data[key]!.keys) {
  finalData[date] = data[key]![date]!   (finalData[date] ?? 0);
}

CodePudding user response:

@pedropimont First of all, how can i put these maps (Map1,Map2.....) to the "data" dynamically that you did manually?

final data = {
    'Map1': {'01.01.2021':2,'02.01.2021':5,'03.01.2021':3}, 
    'Map2': {'02.01.2021':10,'03.01.2021':4,'04.01.2021':8}, 
  };

Second question is that, actually what i am trying to do is to use these values in a graph. To do that, instead of summing the values as I mentioned in the previous question, i need to calculate the percentages.

For example; Every maps are inside a class and inside the class there is an amount value for each map.

for 'Map1': {'01.01.2021':2,'02.01.2021':5,'03.01.2021':3},-> 2 , 5and 3 values are percentages. And also there is a variable for example Map1Amount= 1000€.

for 'Map2': {'02.01.2021':10,'03.01.2021':4,'04.01.2021':8},-> 10 , 4 and 8 values are percentages. And also there is a variable for example Map2Amount= 2000€.

So finally during the iteration that you specified, i need to make the calculation below;

for each date;

((Map1Amount*Map1[date]) (Map2Amount*Map2[date]))*100/(Map1Amount Map2Amount)

for example for date=02.01.2021;

((1000*5) (2000*10))*100/(1000 2000)

For the different keys, again there will be no calculation and store as it is.

Thank you again.

  • Related