Home > OS >  How to write a function with mapping of all objects inside?
How to write a function with mapping of all objects inside?

Time:12-11

I am stuck with one task and I don't understand how to solve it..

I have a map with lists inside:

final Map states = {"Anxiety": [], "Stress": [], "Involvement": [], "Relaxation": [], "Fatigue": []};

And I get data from my Api, that returns a list with maps:

final data = [
  {id: 1, states: {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}},
  {id: 2, states: {Stress: 1, Fatigue: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}}
]

Maximum elements of states is 7.

Api can even returns states: null. There is no persistent data, api can return 2 values(states: {"Anxiety":2, "Stress": 1}), three, etc.

And I want to write a function summarize all integers of elements in states, let's take id:2. I summarize values => Stress(1) Fatigue(1) Involvement(1) Slight Fatigue(2) Anxiety(5) Relaxation(2) Chronic Fatigue(3) == 15

And then I find percentage of each element: Stress: (1 / 15) * 100 == 6.6%, Involvement: (1 / 15) * 100 == 6.6%, Fatigue(Here I Summarize Fatigue, Slight Fatigue,Chronic Fatigue):6 / 15 == 40%, Anxiety: (5 / 15) * 100 == 33.3%, Relaxation: (2 / 15) * 100 == 13.3%

And then I push to Map the values, like this, let's take {"Anxiety": []} for example:

if (Anxiety == 1) then we push to Anxiety array 1, if anxiety >= 25% then we push 2, if anxiety >= 50% then we push 3 else we push 0

And the result of id=2 should be like this:

final Map states = {"Anxiety": [1], "Stress": [1], "Involvement": [1], "Relaxation": [1], "Fatigue": [2]};

And the final result should be like this**(id:1, id:2)**:

final Map states = {"Anxiety": [3,1], "Stress": [2,1], "Involvement": [0,1], "Relaxation": [0,1], "Fatigue": [2,2]};

How to write this function, I tried to map all that stuff but it was getting too complicated and I couldn't achieve any results...

CodePudding user response:

Before I submit my answer I would like to say I agree with pmatatias.

I am assuming your dataset looks like this:

// This is not needed
// final Map states = {
//  "Anxiety": [],
//  "Stress": [],
//  "Involvement": [],
//  "Relaxation": [],
//  "Fatigue": []
//};

final data = [
  {
    "id": 1,
    "states": {"Anxiety": 2, "Stress": 1, "Chronic Fatigue": 1}
  },
  {
    "id": 2,
    "states": {
      "Stress": 1,
      "Fatigue": 1,
      "Involvement": 1,
      "Slight Fatigue": 2,
      "Anxiety": 5,
      "Relaxation": 2,
      "Chronic Fatigue": 3
    }
  }
];

If it's not then you have to use json.decode.

Then extract data from above dataset:

_function(List data) {
  Map<String, dynamic> _result = {};

  for (int i = 0; i < data.length; i  ) {
    _result[data[i]['id'].toString()] = _addUpAndGetPercentage(
       data[i]['states'] != null ? data[i]['states'].keys.toList(): [], data[i]['states'] != null ? data[i]['states'].values.toList(): []);
  }

  return _result;
}

//Function to get percentage
_addUpAndGetPercentage(List<String> keys, List<int> data) {
  int total = 0;
  Map<String, double> _result = {};

  for (int i = 0; i < data.length; i  ) {
    total  = data[i];
  }
  for (int i = 0; i < data.length; i  ) {
    _result[keys[i]] =
        (data[i] / total) * 100; // Multiplying by 100 not necessary you can use (data[i] / total)
  }

  return _result;
}

Then create a function to sort percentage. For example I chose:

if anxiety >= 10% then we push 1, if anxiety >= 25% then we push 2, if anxiety >= 50% then we push 3 else we push 0

// You can change to your own parameters here
int sortPercentage(double percentage) {
  if (percentage >= 10 && percentage < 25) {
    return 1;
  } else if (percentage >= 25 && percentage < 50) {
    return 2;
  } else if (percentage >= 50) {
    return 3;
  } else {
    return 0;
  }
}

//OR

//If you use (data[i] / total)

int sortPercentage(double percentage) {
  if (percentage >= 0.1 && percentage < 0.25) {
    return 1;
  } else if (percentage >= 0.25 && percentage < 0.50) {
    return 2;
  } else if (percentage >= 0.50) {
    return 3;
  } else {
    return 0;
  }
}

Finally extract the data and sort:

getFinalResult(Map<String, dynamic> data) {
  Map<String, dynamic> _result = {};
  int _length = data.entries.toList().length;

  for (int x = 0; x < _length; x  ) {
    var x_data = data.entries.toList()[x];
    var x_data_length = x_data.value.entries.toList().length;

    for (int y = 0; y < x_data_length; y  ) {
      var y_data = data.entries.toList()[x].value.entries.toList()[y];
      if (_result.containsKey(y_data.key)) {
        _result.update(y_data.key, (value) {
          List _list = [];
          _list.addAll(value);
          _list.add(sortPercentage(y_data.value));

          return _list;
        });
      } else {
        _result[y_data.key] = [sortPercentage(y_data.value)];
      }
    }
  }

  return _result;
}

Run the code:

void main() async {
  var midResult = _function(data);
  var finalResult = getFinalResult(midResult);

  print(finalResult);
}

Output Should look like this:

{Anxiety: [3, 2], Stress: [2, 0], Chronic Fatigue: [2, 1], Fatigue: [0], Involvement: [0], Slight Fatigue: [1], Relaxation: [1]}
  • Related