在 Flutter 中查看排序的 Json 数据 Table
View Sorted Json Data in a Flutter Table
[
{
"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 数据。我使用 this-
按 'win_rate' 排序
final data = json.decode(jsonData);
data.sort((a, b) =>
a.values.first["win_rate"].compareTo(b.values.first["win_rate"]));
现在我想在 flutter 中构建一个 table,它匹配最高的 'win_rate' 和最低的 'win_rate' 名字。
如何接近它?
我尝试了 但我最终得到了 errors/no 数据。
只需从您的数据中获取反转的项目并将其放在当前项目的前面:
@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(),
),
),
);
}
您还可以循环排序的数据并使用构建函数创建匹配的数组。
[
{
"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 数据。我使用 this-
按 'win_rate' 排序final data = json.decode(jsonData);
data.sort((a, b) =>
a.values.first["win_rate"].compareTo(b.values.first["win_rate"]));
现在我想在 flutter 中构建一个 table,它匹配最高的 '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(),
),
),
);
}
您还可以循环排序的数据并使用构建函数创建匹配的数组。