How do I retrieve data from the firebase store by a document not a collection in a flutter as a DataRow and DataCell? For example, this image below.
As you can see in the picture, I want to retrieve the data only in the Teabags document but I don't know how please help.
this is an example of my code using collections.
Widget buildDataTable() {
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('Products')
.snapshots(includeMetadataChanges: true),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong! ${snapshot.error}');
} else if (snapshot.hasData) {
final columns = ['Items', 'Price'];
return DataTable(
columnSpacing: 300,
showCheckboxColumn: false,
columns: getcolumns(columns),
rows: _createRows(snapshot.data));
} else {
return const Center(child: CircularProgressIndicator());
}
});
}
List<DataColumn> getcolumns(List<String> columns) => columns
.map((String column) => DataColumn(
label: Text(column, style: Defaults.drawerTextBlacknormal)))
.toList();
List<DataRow> _createRows(QuerySnapshot snapshot) {
List<DataRow> newList = snapshot.docs.map((DocumentSnapshot document) {
Map<String, dynamic> data = document.data()! as Map<String, dynamic>;
return DataRow(
cells: [
DataCell(Text(data['Items'].toString(),
style: Defaults.drawerTextBlacknormal)),
DataCell(Text(data['Price'].toString(),
style: Defaults.drawerTextBlacknormal)),
],
onSelectChanged: ((bool? selected) =>
selected == null ? selected = false : selected = true),
);
}).toList();
return newList;
}
