如何限制flutter中的JSON数组输出?
我有一个JSON数组。我需要在颤振中用一些条件限制它,并在分页数据表小部件中显示它 这是我的JSON数组:如何限制flutter中的JSON数组输出?,json,flutter,Json,Flutter,我有一个JSON数组。我需要在颤振中用一些条件限制它,并在分页数据表小部件中显示它 这是我的JSON数组: var customers = [ { 'Customer_Code': '1001', 'Customer_Name': 'Priston', 'City': 'NewYork', 'Enabled': true, }, { 'Customer_Code': '1002', 'Customer_Name'
var customers = [
{
'Customer_Code': '1001',
'Customer_Name': 'Priston',
'City': 'NewYork',
'Enabled': true,
},
{
'Customer_Code': '1002',
'Customer_Name': 'Harold',
'City': 'NewYork',
'Enabled': true,
},
{
'Customer_Code': '1003',
'Customer_Name': 'Hohn',
'City': 'London',
'Enabled': true,
},
{
'Customer_Code': '1004',
'Customer_Name': 'Schwan',
'City': 'London',
'Enabled': false,
},
{
'Customer_Code': '1005',
'Customer_Name': 'Walter',
'City': 'NewYork',
'Enabled': false,
},
];
这是我的颤振代码:
import 'package:flutter/material.dart';
import 'json/test.dart';
class Testing extends StatefulWidget {
Testing({Key key}) : super(key: key);
@override
_TestingState createState() => _TestingState();
}
class _TestingState extends State<Testing> {
var dts = Dts();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Test'),
),
body: SingleChildScrollView(
child: PaginatedDataTable(
header: Text('Customers'),
columnSpacing: 10,
columns: [
DataColumn(label: Text('Row')),
DataColumn(label: Text('Customer Code')),
DataColumn(label: Text('Customer Name')),
DataColumn(label: Text('City')),
],
source: dts,
rowsPerPage: 15,
),
),
);
}
}
class Dts extends DataTableSource {
@override
DataRow getRow(int index) {
var post = customers[index];
var i = index + 1;
return DataRow.byIndex(index: index,cells: [
DataCell(Text('$i')),
DataCell(Text(post['Customer_Code'])),
DataCell(Text(post['Customer_Name'])),
DataCell(Text(post['City'])),
]);
}
@override
bool get isRowCountApproximate => false;
@override
int get rowCount => customers.length;
@override
int get selectedRowCount => 0;
}
导入“包装:颤振/材料.省道”;
导入“json/test.dart”;
类测试扩展了StatefulWidget{
测试({Key}):超级(Key:Key);
@凌驾
_TestingState createState();
}
类_TestingState扩展状态{
var dts=dts();
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“测试”),
),
正文:SingleChildScrollView(
子项:分页可编辑(
标题:文本(“客户”),
柱间距:10,
栏目:[
DataColumn(标签:Text('Row')),
数据列(标签:文本(“客户代码”),
数据列(标签:文本(“客户名称”),
DataColumn(标签:Text('City')),
],
资料来源:dts,
行页码:15,
),
),
);
}
}
类Dts扩展了DataTableSource{
@凌驾
DataRow getRow(整型索引){
var post=客户[指数];
var i=指数+1;
返回DataRow.byIndex(索引:索引,单元格:[
数据单元(文本(“$i”),
数据单元(文本(post['Customer_Code']),
数据单元(文本(post['Customer_Name']),
数据单元(文本(post['City']),
]);
}
@凌驾
bool get isRowCountApproximate=>false;
@凌驾
int get rowCount=>customers.length;
@凌驾
int get selectedRowCount=>0;
}
结果是:
我需要将其限制为具有以下条件的客户:
city=NewYork
和Enabled=true
我不知道怎么做
我使用
customers
变量作为数据表源,它显示了整个JSON数组 Dart允许使用where筛选列表
我添加了这一行并解决了问题:
var newlst = customers.where((f) => f['City']=='NewYork' && f['Enabled']==true).toList();
现在,结果显示在屏幕截图中:
Dart允许使用where筛选列表 我添加了这一行并解决了问题:
var newlst = customers.where((f) => f['City']=='NewYork' && f['Enabled']==true).toList();
现在,结果显示在屏幕截图中: