flutter 如何写一个函数,里面有所有对象的Map?

vcudknz3  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(120)

我被一个任务卡住了,我不知道如何解决它。
我有一张Map,里面有清单:

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

我从我的API获取数据,它返回一个包含Map的列表:

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,
    },
  },
];

状态的最大元素数为7。
API甚至可以返回states: null.没有持久化数据,API可以返回2个值(states: {"Anxiety":2, "Stress": 1})、3个值等。
我想写一个函数来总结状态中所有元素的整数,让我们取id:2。我总结的值=〉压力(1)+疲劳(1)+参与(1)+轻微疲劳(2)+焦虑(5)+放松(2)+慢性疲劳(3)== 15
然后我找到每种元素的百分比:压力:(1 / 15) 100 == 6.6%,参与度:(1 / 15) 100 == 6.6%,疲劳(这里我总结疲劳、轻微疲劳、慢性疲劳):6 / 15 == 40%,焦虑:(5 / 15)* 100 == 33.3%,松弛:(2 / 15)× 100 == 13.3% × ×
然后我按下Map the values,就像这样,以{"Anxiety": []}为例:
如果(Anxiety == 1),则我们推到Anxiety数组1,如果Anxiety〉= 25%,则我们推2,如果Anxiety〉= 50%,则我们推3,否则我们推0
id=2的结果应该是这样的:

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

而最后的结果应该是这样的**(id:1,id:2)**:

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

如何写这个函数,我试图Map所有的东西,但它变得太复杂,我不能达到任何结果...

2w2cym1i

2w2cym1i1#

在我提交我的答案之前,我想说的是,我同意pmatatias。
我假设您的数据集如下所示:

// 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
    }
  }
];

如果不是,则必须使用json.decode
然后从上述数据集中提取数据:

_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;
}

然后创建一个函数来排序百分比。例如我选择:

如果焦虑〉= 10%,则按1,如果焦虑〉= 25%,则按2,如果焦虑〉= 50%,则按3,否则按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;
  }
}

最后提取数据并排序:

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;
}

运行代码:

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

  print(finalResult);
}

输出应如下所示:

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

相关问题