问题
How to populate a DataTable using StreamBuilder?
Below is my code:
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),
);
},
)
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.
回答1:
I dont know what data comes from your stream, but here is on idea on how to do it:
DataTable(
rows: _createRows(snapshot.data),
)
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;
}
You can apply the same logic for cell creation, or you can do it inside the first map function as well.
来源:https://stackoverflow.com/questions/50733005/filling-a-datatable-with-firestore-and-flutter-using-streambuilder