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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With