Writing my first Flutter application and we need to use a PaginatedDataTable. The docs say the source field should

generally have a lifetime longer than the PaginatedDataTable widget itself; it should be reused each time the PaginatedDataTable constructor is called.


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.

Can you also explain why the DataTableSource should be reused? Thanks.

Solution 1: Rémi Rousselet

DataTableSource is the state of your Table. 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. This is especially true considering data is lazy loaded, and potentially comes from http call.

Ideally you'll want to store your DataSource inside a StatefulWidget or something similar (InheritedWidget, a Stream, whatever).

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

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