【发布时间】:2021-10-30 12:54:36
【问题描述】:
在代码中,代码以实时(流)方式从 Cloud Firestore 获取数据,并使用 StreamBuilder 在数据表小部件中显示它,但是当我运行代码时,它给出了我上面所问的错误.
SizedBox(
width: double.infinity,
child: StreamBuilder(
stream: FirebaseFirestore.instance.collection('products').snapshots(),
builder: (BuildContext context,snapshot) {
(!snapshot.hasData)?
Center(child: CircularProgressIndicator())
: DataTable(
// columnSpacing: defaultPadding,
columns: [
DataColumn(label: Text("Id")),
DataColumn(label: Text("Name")),
DataColumn(label: Text("Category")),
DataColumn(label: Text("Image")),
DataColumn(label: Text("Original Price")),
DataColumn(label: Text("Sale Price")),
DataColumn(label: Text("Discount")),
DataColumn(label: Text("Commission")),
DataColumn(label: Text("Date")),
],
rows: _listofRows(snapshot.data),
);
})),
_listofRows 方法代码在这里
List<DataRow> _listofRows(snapshot) {
List<DataRow> newList = snapshot.docs.map((docSnapshot) {
return DataRow(cells: <DataCell>[
DataCell(Text(docSnapshot.data()['ProductID'].toString())),
DataCell(Text(docSnapshot.data()['Product Name'].toString())),
DataCell(Text(docSnapshot.data()['Category Name'].toString())),
DataCell(Text(docSnapshot.data()['Product ImageUrl'].toString())),
DataCell(Text(docSnapshot.data()['originalPrice'].toString())),
DataCell(Text(docSnapshot.data()['salePrice'].toString())),
DataCell(Text(docSnapshot.data()['Discount'].toString())),
DataCell(Text(docSnapshot.data()['Commission Rate'].toString())),
DataCell(Text(doctSnapshot.data()['Out of Stock Date'].toString())),
]);
}).toList();
return newList;
}
【问题讨论】: