【发布时间】:2020-08-22 19:48:10
【问题描述】:
通常,我们在 Flutter 项目中使用 StreamProvider 来处理来自 FireStore 的数据,如下所示:
// I have a collection of Customer in my DataService
Stream<List<Client>> streamCustomers() {
return Firestore.instance.collection('customers').snapshots().map((list) =>
list.documents.map((doc) => Customer.fromMap(doc.data, doc.documentID)).toList());
}
这是流数据的提供者:
Expanded(
child: StreamProvider<List<Customer>>.value(
value: _dataSvc.streamCustomers(),
child: CustomerListWidget(),
),
);
这是流数据消耗的地方:
final _customers = Provider.of<List<Customer>>(context);
return Container(
child: _customers == null? Text('Loading...') : _buildList(context, _customers),
);
我将在CustomerListWidget 中显示所有客户数据。因为Customer 集合的数据非常大(超过10,000 - 50,000 条记录)。显然这不是有效的解决方案。我想知道在 Flutter/Firestore 项目中通常使用什么样的实用解决方案来处理这种情况?
P.S.:分页绝对是我可能的选择之一。但是有一些问题,因为我将对数据应用一些过滤器。例如,每次更改过滤条件时,我都必须查询 Firestore,这将导致数据使用量增加。而且似乎我只能使用getDocuments() 而不是snapshot 来获取流数据。
【问题讨论】:
-
实现分页。
标签: firebase flutter google-cloud-firestore