分页数据表和提供者
PaginatedDataTable & Provider
我的 PaginatedDataTable
小部件:
var shopData = ShopDataSource();
return PaginatedDataTable(
header: Text('Shops'),
columns: [
DataColumn(label: Text('Shop')),
DataColumn(label: Text('Type')),
DataColumn(label: Text('Location')),
DataColumn(label: Text('Status')),
DataColumn(label: Text('Actions')),
],
rowsPerPage: 5,
source: shopData,
);
ShopDataSource
class 扩展 DataTableSource
像这样:
class ShopDataSource extends DataTableSource {
var _shops;
// fetch data from provider
var docProvider = Provider.of(context);
DataRow getRow(int index) {
return DataRow.byIndex(cells: [
// dataCells
], index: index);
}
@override
bool get isRowCountApproximate => false;
@override
int get rowCount => _shops.length;
@override
int get selectedRowCount => 0;
}
我的问题:如何从 DataProvider class 获取数据到 DataCell
s。
或
使用 StreamBuilder
或不使用此从 Firestore
获取数据的任何替代方法。
经过一番研究,我可以用
解决这个问题
class ShopTable extends StatelessWidget {
const ShopTable({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
List<Shop> shops;
var docProvider = Provider.of<DocumentProvider>(context);
return StreamBuilder(
stream: docProvider.fetchShopsAsStream(),
builder: (context, AsyncSnapshot<QuerySnapshot> snap) {
shops = snap.data.documents.map((e) => Shop.fromMap(e.data)).toList();
var shopData = ShopDataSource(shops);
if (snap.hasData) {
return PaginatedDataTable(
header: Text('Shops'),
columns: [
DataColumn(label: Text('Shop')),
DataColumn(label: Text('Type')),
DataColumn(label: Text('Location')),
DataColumn(label: Text('Status')),
DataColumn(label: Text('Actions')),
],
rowsPerPage: 5,
source: shopData,
);
}
return LinearProgressIndicator();
},
);
}
}
class ShopDataSource extends DataTableSource {
final List<Shop> shops;
ShopDataSource(this.shops);
DataRow getRow(int index) {
return DataRow.byIndex(cells: [
DataCell(Text(shops[index].shopName)),
DataCell(Text(shops[index].phoneNumber)),
DataCell(Text(shops[index].email)),
DataCell(Text(shops[index].shopOwner)),
DataCell(Text(shops[index].shopAddress)),
], index: index);
}
@override
bool get isRowCountApproximate => false;
@override
int get rowCount => shops.length;
@override
int get selectedRowCount => 0;
}
我的 PaginatedDataTable
小部件:
var shopData = ShopDataSource();
return PaginatedDataTable(
header: Text('Shops'),
columns: [
DataColumn(label: Text('Shop')),
DataColumn(label: Text('Type')),
DataColumn(label: Text('Location')),
DataColumn(label: Text('Status')),
DataColumn(label: Text('Actions')),
],
rowsPerPage: 5,
source: shopData,
);
ShopDataSource
class 扩展 DataTableSource
像这样:
class ShopDataSource extends DataTableSource {
var _shops;
// fetch data from provider
var docProvider = Provider.of(context);
DataRow getRow(int index) {
return DataRow.byIndex(cells: [
// dataCells
], index: index);
}
@override
bool get isRowCountApproximate => false;
@override
int get rowCount => _shops.length;
@override
int get selectedRowCount => 0;
}
我的问题:如何从 DataProvider class 获取数据到 DataCell
s。
或
使用 StreamBuilder
或不使用此从 Firestore
获取数据的任何替代方法。
经过一番研究,我可以用
解决这个问题class ShopTable extends StatelessWidget {
const ShopTable({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
List<Shop> shops;
var docProvider = Provider.of<DocumentProvider>(context);
return StreamBuilder(
stream: docProvider.fetchShopsAsStream(),
builder: (context, AsyncSnapshot<QuerySnapshot> snap) {
shops = snap.data.documents.map((e) => Shop.fromMap(e.data)).toList();
var shopData = ShopDataSource(shops);
if (snap.hasData) {
return PaginatedDataTable(
header: Text('Shops'),
columns: [
DataColumn(label: Text('Shop')),
DataColumn(label: Text('Type')),
DataColumn(label: Text('Location')),
DataColumn(label: Text('Status')),
DataColumn(label: Text('Actions')),
],
rowsPerPage: 5,
source: shopData,
);
}
return LinearProgressIndicator();
},
);
}
}
class ShopDataSource extends DataTableSource {
final List<Shop> shops;
ShopDataSource(this.shops);
DataRow getRow(int index) {
return DataRow.byIndex(cells: [
DataCell(Text(shops[index].shopName)),
DataCell(Text(shops[index].phoneNumber)),
DataCell(Text(shops[index].email)),
DataCell(Text(shops[index].shopOwner)),
DataCell(Text(shops[index].shopAddress)),
], index: index);
}
@override
bool get isRowCountApproximate => false;
@override
int get rowCount => shops.length;
@override
int get selectedRowCount => 0;
}