简体   繁体   English

管理PaginatedDataTable的DataTableSource的最佳方法?

[英]Best way to manage DataTableSource for PaginatedDataTable?

Writing my first Flutter application and we need to use a PaginatedDataTable . 编写我的第一个Flutter应用程序,我们需要使用PaginatedDataTable The docs say the source field should 文档说source字段应该

generally have a lifetime longer than the PaginatedDataTable widget itself; 通常,其生存期比PaginatedDataTable小部件本身更长; it should be reused each time the PaginatedDataTable constructor is called. 每次调用PaginatedDataTable构造函数时,都应重用它。

https://docs.flutter.io/flutter/material/PaginatedDataTable/source.html https://docs.flutter.io/flutter/material/PaginatedDataTable/source.html

What is the best way to manage this? 最好的管理方式是什么? Is there a common pattern? 有共同的模式吗? My initial thought is the singleton pattern but I come from the Java world so I'm not sure if this is correct. 我最初的想法是单例模式,但是我来自Java世界,因此不确定是否正确。

Can you also explain why the DataTableSource should be reused? 您还可以解释为什么应重新使用DataTableSource吗? Thanks. 谢谢。

DataTableSource is the state of your Table. DataTableSource是表的状态。 It contains all your table data and whether or not rows are selected. 它包含您所有的表数据以及是否选择了行。

It must be persisted somewhere because you'd loose all your selection and potential loaded data if you recreated your DataSource anew everytime. 它必须保留在某个地方,因为如果每次重新创建DataSource时,您都会失去所有选择和可能加载的数据。 This is especially true considering data is lazy loaded, and potentially comes from http call. 考虑到数据是延迟加载的,并且可能来自http调用,因此尤其如此。

Ideally you'll want to store your DataSource inside a StatefulWidget or something similar (InheritedWidget, a Stream, whatever). 理想情况下,您需要将DataSource存储在StatefulWidget或类似的东西(InheritedWidget,Stream等)中。

class MyTable extends StatefulWidget {
  @override
  _MyTableState createState() => new _MyTableState();
}

class _MyTableState extends State<MyTable> {
  final myDataSource = new MyDataSource();

  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM