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类扩展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 类获取数据到DataCells.

或者

Firestore 任何从有StreamBuilder或没有此获取数据的替代方式。

4

1 回答 1

1

经过一番研究,我可以解决这个问题

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;
}
于 2020-08-08T15:57:48.230 回答