简体   繁体   中英

Best way to manage DataTableSource for PaginatedDataTable?

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.

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.

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

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 {
  @override
  _MyTableState createState() => new _MyTableState();
}

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

  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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