用Firestore和Flutter填充数据表(使用StreamBuilder) [英] Filling a DataTable with Firestore and Flutter (using StreamBuilder)
本文介绍了用Firestore和Flutter填充数据表(使用StreamBuilder)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用StreamBuilder填充数据表?
How to populate a DataTable using StreamBuilder?
下面是我的代码:
new StreamBuilder(
stream: widget._returnStreamWithActiveKeysOnly(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new DataTable(
columns: <DataColumn>[
new DataColumn(
label: Text('type'),
tooltip: 'Ordinary or service (1 day only, restricted time)',
),
new DataColumn(label: Text('Key')),
new DataColumn(label: Text('Check-in')),
new DataColumn(label: Text('Check-out')),
],
rows: _listOfRows(snapshot),
);
},
)
我找不到一种方法(例如内置生成器)来指示文档索引传递给 _listOfRows
函数,或如何访问每个流的当前文档.
I couldn't find a way (like a built-in builder) to indicate the document index to pass to the _listOfRows
function, or how to access the current document for each stream.
推荐答案
我不知道什么数据来自您的流,但是下面是有关如何做的想法:
I dont know what data comes from your stream, but here is on idea on how to do it:
DataTable(
rows: _createRows(snapshot.data),
)
您希望您的构建器方法返回 List< DataRow>
You want your builder method to return List<DataRow>
List<DataRow> _createRows(QuerySnapshot snapshot) {
List<DataRow> newList = snapshot.documents.map((DocumentSnapshot documentSnapshot) {
return new DataRow(cells: _createCellsForElement(documentSnapshot["someDataYouWantToProcessForCellData"]));
}).toList();
return newList;
}
您可以将相同的逻辑应用于单元格的创建,也可以在第一个map函数中进行.
You can apply the same logic for cell creation, or you can do it inside the first map function as well.
这篇关于用Firestore和Flutter填充数据表(使用StreamBuilder)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文