首页 > 解决方案 > 如何在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,
            ),
          ],
        ),
      ),
    );
  }
}

标签: firebasefluttergoogle-cloud-firestore

解决方案


推荐阅读