首页 > 解决方案 > 在 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' 名称。如何接近它?我试过这个,但我最终得到错误/没有数据。

标签: jsonflutterdartflutter-table

解决方案


只需从您的数据中获取反转的项目并将其放在当前项目的前面:

@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(),
      ),
    ),
  );
}

您还可以循环排序数据并从构建函数中创建匹配的数组。


推荐阅读