How do i retrieve data from firebase store by document not collection in flutter as a DataRow and DataCell. For example, this image below.
As you can see i in the picture, i want to retrieve the data only in 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;
}
The following line will return the stream of the document with id Teabags :
FirebaseFirestore.instance.collection("Products").doc("Teabags).snapshots()
You were missing the " .doc("{id}")
" function.
You should be able to retrieve the data in the following way:
String items = snapshot.data()["Items];
int price = snapshot.data()["Price"];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.