首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PaginatedDataTable & Provider

PaginatedDataTable & Provider
EN

Stack Overflow用户
提问于 2020-08-08 15:20:44
回答 1查看 505关注 0票数 0

我的PaginatedDataTable部件:

代码语言:javascript
复制
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

代码语言:javascript
复制
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类获取到DataCell类。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-08 15:57:48

经过一番研究,我可以用

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63317040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档