【问题标题】:PaginatedDataTable & Provider分页数据表和提供者
【发布时间】:2020-11-28 16:35:12
【问题描述】:

我的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。

使用StreamBuilder 或不使用此方法从Firestore 获取数据的任何替代方式。

【问题讨论】:

  • 嗨,有没有办法在 onSelectChange 中为复选框使用 setstate。我可以得到列表,但问题是检查 ae 没有在 UI 中更新

标签: flutter google-cloud-firestore paginateddatatable


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    相关资源
    最近更新 更多