Firestore 有一个我在 中显示的集合DataTable
。还有一个 onTap 函数,我试图通过它显示我的产品数据。数据应显示在 中showDialog()
。
return StreamBuilder<List<Product>>(
stream: ProductService().products,
builder: (context, snapshot) {
if (snapshot.hasData) {
return DataTable(
columns: [
DataColumn(
label: Text('Наименование'),
),
],
rows: snapshot.data.map((products) {
return DataRow(cells: [
DataCell(
Text(products.name),
onTap: () => showDialog(
context: context,
builder: (context) => ProductDetails(),
)
),
DataCell(Text(products.date)),
DataCell(Text(products.category)),
]);
}).toList(),
);
showDialog 返回一个 ProductDetails 小部件。
Widget build(BuildContext context) {
return StreamBuilder<Product>(
stream: ProductService().productDetails,
builder: (context, snapshot) {
if (snapshot.hasData) {
Product productDetails = snapshot.data;
return Dialog(
child: Container(
width: 500,
height: 500,
child: Form(
key: _formProductDetails,
child: Column(
children: <Widget>[
TextFormField(
initialValue: productDetails.name,
),
],
),
),
),
);
else {
return Text('error');
}
产品类别
class ProductService {
var dbTimeKey = DateTime.now();
var formatDate = DateFormat('dd-MM-yyyy HH:mm:ss');
final CollectionReference _firestore =
Firestore.instance.collection("products");
Future addProduct(_nameproductController, _priceproductController,
_productQuantityController, _currentCategory, url, bool isActive) async {
String date = formatDate.format(dbTimeKey);
_firestore.document(date).setData({
'name': _nameproductController,
'price': _priceproductController,
'category': _currentCategory,
'image': url,
'date': date,
'quantity': _productQuantityController,
'isActive': true,
});
}
List<Product> _productList(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
return Product(
name: doc.data['name'] ?? '',
price: doc.data['price'] ?? '0',
category: doc.data['category'] ?? '',
quantity: doc.data['quantity'] ?? '0',
date: doc.data['date'] ?? '',
image: doc.data['image'] ?? '',
isActive: doc.data['isActive'] ?? '',
);
}).toList();
}
Stream<List<Product>> get products {
return _firestore.snapshots().map(_productList);
}
Product productDetailsFromSnap(DocumentSnapshot snapshot) {
return Product(
name: snapshot.data['name'],
price: snapshot.data['price'],
category: snapshot.data['category'],
quantity: snapshot.data['quantity'],
date: snapshot.data['date'],
image: snapshot.data['image'],
isActive: snapshot.data['isActive'],
);
}
Stream<Product> get productDetails {
return _firestore.document().snapshots().map(productDetailsFromSnap);
}
}
问题是当您单击产品名称时,它应该打开一个窗口并显示产品的详细信息,它会返回错误。productdetails 小部件不了解从哪个文档中提取信息。
它会是这样的(我是凭记忆写的,也许在某个地方会有你可以纠正的错误)。如果您有任何问题,请询问!
主要的:
产品详情:
产品服务: