json - 在 Flutter 表中查看排序后的 Json 数据
问题描述
[
{
"1": {
"name": "Shahed Emon",
"win_rate": "98.7%"
}
},
{
"2": {
"name": "Mustakim Nahid",
"win_rate": "88.7%"
}
},
{
"3": {
"name": "Imtiaz Rizan",
"win_rate": "72.3%"
}
},
{
"4": {
"name": "Ishtiak Rongon",
"win_rate": "52.6%"
}
}
]
我有这个 json 数据。我使用这个按“win_rate”排序-
final data = json.decode(jsonData);
data.sort((a, b) =>
a.values.first["win_rate"].compareTo(b.values.first["win_rate"]));
现在我想在颤振中建立一个表,它匹配最高的 'win_rate' 和最低的 'win_rate' 名称。如何接近它?我试过这个,但我最终得到错误/没有数据。
解决方案
只需从您的数据中获取反转的项目并将其放在当前项目的前面:
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.yellow,
child: Table(
border: TableBorder(horizontalInside: BorderSide(width: 1)),
children: _sortedData.map(
(item) {
final reverseItem = _sortedData[
_sortedData.length - _sortedData.indexOf(item) - 1];
return TableRow(
children: [
TableCell(
child: Text(item.values.first['name']),
),
TableCell(
child: Text(reverseItem.values.first['name']),
)
],
);
},
).toList(),
),
),
);
}
您还可以循环排序数据并从构建函数中创建匹配的数组。
推荐阅读
- r - Combination of reshaping and aggregating a data.frame
- html - 必需的属性在 React 表单中不起作用
- sql - 如何创建可以提高图表性能的聚合表(数据集市)?
- algorithm - 给定序列具有值的概率
- unit-testing - Mockito:模拟一个可调用的参数
- json - Bash循环通过CSV但用换行符留下最后一行值
- node.js - MongoDB 副本集在 DESKTOP 而不是 localhost 上启动
- php - CSV 联赛不跳过空记录
- sql - 是否有任何方法可以找到数据库中具有特定值的列的所有表,例如。EMP_NAME = ABC?
- c# - 未处理的异常:System.IO.IOException:句柄在 asp.net mvc core 3.1 应用程序中无效