firebase - 如何在flutter中基于其他集合document.id从集合firebase中获取价值
问题描述
在那里,我从类别文档中检索了 Products 集合。问题是当我在 print 中使用 document.id 时,它正在工作。但是当我像这样使用 document.id 时它不起作用。正如我在代码中提到的,
productId: 文档.id,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.doc(document.id) .......... ..................................................... ..................................................... ..................................................... ..................................................... ………………………………………………………………………………………………………………………………………… _ ..................................................... ..................................................... ..................................................... ..................................................... …………………………………………………………………………………………………………………………………………
class CategoryConn extends StatefulWidget {
final String categoryid;
CategoryConn({this.categoryid});
@override
State<CategoryConn> createState() => _CategoryConnState();
}
class _CategoryConnState extends State<CategoryConn> {
final FirebaseServices _firebaseServices = FirebaseServices();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Stack(
children: [
FutureBuilder<QuerySnapshot>(
future: _firebaseServices.categoryRef
.doc("${widget.categoryid}")
.collection("Products")
.get(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Scaffold(
body: Center(
child: Text("Error: ${snapshot.error}"),
),
);
}
// Collection Data ready to display
if (snapshot.connectionState == ConnectionState.done) {
// Display the data inside a list view
return ListView(
padding: EdgeInsets.only(
top: 108.0,
bottom: 12.0,
),
children: snapshot.data.docs.map((document) {
final dynamic data = document.data();
return GestureDetector(
onTap: () async {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProductPage(
// here the document.id is not working
productId: document.id,
),
));
},
child: FutureBuilder(
future: _firebaseServices.productsRef
// here the document.id is not working
.doc(document.id)
.get(),
builder: (context, newsnapshot) {
if (newsnapshot.hasError) {
return Scaffold(
body: Center(
child: Text("Error: ${newsnapshot.error}"),
),
);
}
// but in here it is printing the document.id
print("object thisisss${document.id}");
if (newsnapshot.connectionState ==
ConnectionState.done) {
// Firebase Document Data Map
Map<String, dynamic> documentData =
newsnapshot.data.data();
// print("object${documentData['name']}");
return GridView.count(
crossAxisCount: 2,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
childAspectRatio: .53,
mainAxisSpacing: 8.0,
crossAxisSpacing: 10,
padding: EdgeInsets.only(
top: 10.0,
bottom: 12.0,
left: 5,
right: 5),
children: [
ProductCard(
title: documentData['name'],
)
]);
}
return Container(
child: Center(
child: CircularProgressIndicator(),
),
);
},
),
);
}).toList(),
);
}
// Loading State
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
},
),
CustomActionBar(
title: "Saved",
hasBackArrrow: false,
),
],
),
),
);
}
}
解决方案
推荐阅读
- multithreading - java.util.concurrent.RejectedExecutionException:被 java.util.concurrent.ThreadPoolExecutor 拒绝
- tensorflow - 使用 TF-lite 将 MobileFacenet 转换为量化感知模型时,create_training_graph() 失败
- python - 如何在 Flask SQLAlchemy 中通过用户名获取用户位置的 api 调用路由
- python - 图片从谷歌下载并保存
- java - 未捕获的可抛出 java.lang.AbstractMethodError:org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.createArrayOf
- node.js - 需要帮助以使用 ejs+Node.js 创建调查
- php - 如何将文件(pdf)从本地计算机发送到 API Whatsapp(聊天 API)
- javascript - 如何使用令牌在 Angular 材料表中设置服务器端分页
- python - 如何使用py2neo在neo4j中将列表列表加载为字符串
- python - 如何重新加载python包?TypeError: super(type, obj): obj 必须是类型的实例或子类型