如何从Firestore扑朔迷离中从参考字段中获取值并在Stream Builder中显示?

发布于 2025-02-06 03:40:33 字数 11321 浏览 2 评论 0原文

我有一个名为“订单”的Firestore系列。它的用户详细信息为字段名称“ UID”。字段“ UID”是对“用户”集合中存在的用户数据的引用。不,我必须显示用户名以及订单详细信息。因此,我需要从参考字段获取数据。我正在使用StreamBuilder显示数据。这是我所做的:

 Widget build(BuildContext context) {
    Map UserSnapshot = Map();
    return Scaffold(
        body: StreamBuilder(
        stream:_db.collection('users').snapshots(),
          builder: (BuildContext context, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> UsersSnapshot) {
                  UsersSnapshot.data?.docs.forEach((element) {
                    UserSnapshot[element.id] = element;
                  });
            return StreamBuilder(
                stream:_db.collection('orders').where(
                    'driverId', isEqualTo: sp.getString('uid')).snapshots(),
                builder: (BuildContext context,
                    AsyncSnapshot<QuerySnapshot> OrdersSnapshot) {
                  if (!OrdersSnapshot.hasData) return const Text('Loading...');
                  return ListView.builder(
                      itemCount: OrdersSnapshot.data!.docs.length,
                      itemBuilder:(context,index) {
                        var documentSnapshot= OrdersSnapshot.data!.docs[index].data() as Map<String,dynamic>;
                        return Padding(
                          padding: EdgeInsets.symmetric(vertical: 20),
                          child: Container(
                            width: MediaQuery.of(context).size.width,
                            height: 260,
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(8),
                                border: Border.all(color: Colors.black38, width: 1)),
                            child: Column(
                              children: [
                                Row(
                                  children: [
                                    Padding(
                                      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                                      child: Container(
                                          height: 60,
                                          width: 60,
                                          decoration: BoxDecoration(
                                              borderRadius: BorderRadius.circular(6),
                                              border: Border.all(color: Colors.black12)),
                                          child: Image.network(
                                              'https://indiaeducationdiary.in/wp-content/uploads/2020/10/IMG-20201024-WA0014.jpg')),
                                    ),
                                    Container(
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            'Paradise Multicuisine Restaurant',
                                            style: GoogleFonts.poppins(
                                                fontWeight: FontWeight.bold, fontSize: 14),
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                          Text(
                                            '372,Kamarajar Salai, Karaikal',
                                            style: GoogleFonts.poppins(
                                                fontWeight: FontWeight.w400,
                                                fontSize: 11,
                                                color: hexStringToColor('636567')),
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                                DottedLine(
                                  dashColor: Colors.black26,
                                  dashGapLength: 10,
                                ),
                                Padding(
                                  padding: EdgeInsets.all(20),
                                  child: Container(
                                    width: MediaQuery.of(context).size.height,
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Row(
                                          children: [
                                            Icon(Icons.account_circle),
                                            SizedBox(
                                              width: 7,
                                            ),
                                            Text(
                                              UserSnapshot[documentSnapshot['uid']['fullname']] ?? 'Deleted User',
                                              style: GoogleFonts.poppins(
                                                  fontSize: 12,
                                                  fontWeight: FontWeight.w500,
                                                  color: hexStringToColor('636567')),
                                            ),
                                            SizedBox(width: 3),
                                            Text(
                                              '#'+ documentSnapshot['orderId'],
                                              style: GoogleFonts.poppins(
                                                  fontSize: 12,
                                                  fontWeight: FontWeight.w500,
                                                  color: hexStringToColor('636567')),
                                            )
                                          ],
                                        ),
                                        Container(
                                          width: MediaQuery.of(context).size.width * 0.90,
                                          child: Row(
                                            children: [
                                              Icon(Icons.location_on_rounded),
                                              SizedBox(
                                                width: 7,
                                              ),
                                              Expanded(
                                                child: Text(
                                                  '6/8, RR Colony, Ambedkar Street, Karaikal',
                                                  style: GoogleFonts.poppins(
                                                      fontSize: 12,
                                                      fontWeight: FontWeight.w500,
                                                      color: hexStringToColor('636567')),
                                                  overflow: TextOverflow.clip,
                                                  maxLines: 4,
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                        SizedBox(height: 20,),
                                        Row(
                                          children: [
                                            Container(
                                              decoration: BoxDecoration(
                                                  color: hexStringToColor('aeaeae'),
                                                  borderRadius: BorderRadius.circular(5)
                                              ),
                                              width: 80,
                                              height: 20,
                                              child: Row (
                                                mainAxisAlignment: MainAxisAlignment.center,
                                                children: [
                                                  Text('3 Items',
                                                    style: GoogleFonts.poppins(fontSize: 10, fontWeight: FontWeight.w500),
                                                  )
                                                ],
                                              ),
                                            ),
                                            SizedBox(width: 20,),
                                            Container(
                                              decoration: BoxDecoration(
                                                  color: hexStringToColor('aeaeae'),
                                                  borderRadius: BorderRadius.circular(5)
                                              ),
                                              width: 80,
                                              height: 20,
                                              child: Row (
                                                mainAxisAlignment: MainAxisAlignment.center,
                                                children: [
                                                  Text("₹"+documentSnapshot['grandTotal'].toString(),
                                                    style: GoogleFonts.poppins(fontSize: 10, fontWeight: FontWeight.w500),
                                                  )
                                                ],
                                              ),
                                            ),
                                          ],
                                        ),
                                        Align(
                                          child: ElevatedButton(onPressed: ()=>{},
                                            child: Text('View Details'),
                                            style: ElevatedButton.styleFrom(
                                                elevation: 3,
                                                shape: RoundedRectangleBorder(
                                                    borderRadius: BorderRadius.circular(5)
                                                ),
                                                textStyle: GoogleFonts.poppins(fontWeight: FontWeight.w400,fontSize: 14)
                                            ),
                                          ),
                                          alignment: Alignment.centerRight,
                                        )
                                      ],
                                    ),
                                  ),
                                )
                              ],
                            ),
                          ),
                        );
                      }
                  );
                }
            );

          },
  )
  );
}}

i have a firestore collection named 'orders'. It has user details as field name 'uid'. The field 'uid' is a reference to user data present in 'users' collections. No i have to display username along with order details. So i need to get data from the reference field. i'm using Streambuilder to display data. Here is what i did:

 Widget build(BuildContext context) {
    Map UserSnapshot = Map();
    return Scaffold(
        body: StreamBuilder(
        stream:_db.collection('users').snapshots(),
          builder: (BuildContext context, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> UsersSnapshot) {
                  UsersSnapshot.data?.docs.forEach((element) {
                    UserSnapshot[element.id] = element;
                  });
            return StreamBuilder(
                stream:_db.collection('orders').where(
                    'driverId', isEqualTo: sp.getString('uid')).snapshots(),
                builder: (BuildContext context,
                    AsyncSnapshot<QuerySnapshot> OrdersSnapshot) {
                  if (!OrdersSnapshot.hasData) return const Text('Loading...');
                  return ListView.builder(
                      itemCount: OrdersSnapshot.data!.docs.length,
                      itemBuilder:(context,index) {
                        var documentSnapshot= OrdersSnapshot.data!.docs[index].data() as Map<String,dynamic>;
                        return Padding(
                          padding: EdgeInsets.symmetric(vertical: 20),
                          child: Container(
                            width: MediaQuery.of(context).size.width,
                            height: 260,
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(8),
                                border: Border.all(color: Colors.black38, width: 1)),
                            child: Column(
                              children: [
                                Row(
                                  children: [
                                    Padding(
                                      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                                      child: Container(
                                          height: 60,
                                          width: 60,
                                          decoration: BoxDecoration(
                                              borderRadius: BorderRadius.circular(6),
                                              border: Border.all(color: Colors.black12)),
                                          child: Image.network(
                                              'https://indiaeducationdiary.in/wp-content/uploads/2020/10/IMG-20201024-WA0014.jpg')),
                                    ),
                                    Container(
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            'Paradise Multicuisine Restaurant',
                                            style: GoogleFonts.poppins(
                                                fontWeight: FontWeight.bold, fontSize: 14),
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                          Text(
                                            '372,Kamarajar Salai, Karaikal',
                                            style: GoogleFonts.poppins(
                                                fontWeight: FontWeight.w400,
                                                fontSize: 11,
                                                color: hexStringToColor('636567')),
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                                DottedLine(
                                  dashColor: Colors.black26,
                                  dashGapLength: 10,
                                ),
                                Padding(
                                  padding: EdgeInsets.all(20),
                                  child: Container(
                                    width: MediaQuery.of(context).size.height,
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Row(
                                          children: [
                                            Icon(Icons.account_circle),
                                            SizedBox(
                                              width: 7,
                                            ),
                                            Text(
                                              UserSnapshot[documentSnapshot['uid']['fullname']] ?? 'Deleted User',
                                              style: GoogleFonts.poppins(
                                                  fontSize: 12,
                                                  fontWeight: FontWeight.w500,
                                                  color: hexStringToColor('636567')),
                                            ),
                                            SizedBox(width: 3),
                                            Text(
                                              '#'+ documentSnapshot['orderId'],
                                              style: GoogleFonts.poppins(
                                                  fontSize: 12,
                                                  fontWeight: FontWeight.w500,
                                                  color: hexStringToColor('636567')),
                                            )
                                          ],
                                        ),
                                        Container(
                                          width: MediaQuery.of(context).size.width * 0.90,
                                          child: Row(
                                            children: [
                                              Icon(Icons.location_on_rounded),
                                              SizedBox(
                                                width: 7,
                                              ),
                                              Expanded(
                                                child: Text(
                                                  '6/8, RR Colony, Ambedkar Street, Karaikal',
                                                  style: GoogleFonts.poppins(
                                                      fontSize: 12,
                                                      fontWeight: FontWeight.w500,
                                                      color: hexStringToColor('636567')),
                                                  overflow: TextOverflow.clip,
                                                  maxLines: 4,
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                        SizedBox(height: 20,),
                                        Row(
                                          children: [
                                            Container(
                                              decoration: BoxDecoration(
                                                  color: hexStringToColor('aeaeae'),
                                                  borderRadius: BorderRadius.circular(5)
                                              ),
                                              width: 80,
                                              height: 20,
                                              child: Row (
                                                mainAxisAlignment: MainAxisAlignment.center,
                                                children: [
                                                  Text('3 Items',
                                                    style: GoogleFonts.poppins(fontSize: 10, fontWeight: FontWeight.w500),
                                                  )
                                                ],
                                              ),
                                            ),
                                            SizedBox(width: 20,),
                                            Container(
                                              decoration: BoxDecoration(
                                                  color: hexStringToColor('aeaeae'),
                                                  borderRadius: BorderRadius.circular(5)
                                              ),
                                              width: 80,
                                              height: 20,
                                              child: Row (
                                                mainAxisAlignment: MainAxisAlignment.center,
                                                children: [
                                                  Text("₹"+documentSnapshot['grandTotal'].toString(),
                                                    style: GoogleFonts.poppins(fontSize: 10, fontWeight: FontWeight.w500),
                                                  )
                                                ],
                                              ),
                                            ),
                                          ],
                                        ),
                                        Align(
                                          child: ElevatedButton(onPressed: ()=>{},
                                            child: Text('View Details'),
                                            style: ElevatedButton.styleFrom(
                                                elevation: 3,
                                                shape: RoundedRectangleBorder(
                                                    borderRadius: BorderRadius.circular(5)
                                                ),
                                                textStyle: GoogleFonts.poppins(fontWeight: FontWeight.w400,fontSize: 14)
                                            ),
                                          ),
                                          alignment: Alignment.centerRight,
                                        )
                                      ],
                                    ),
                                  ),
                                )
                              ],
                            ),
                          ),
                        );
                      }
                  );
                }
            );

          },
  )
  );
}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪少女 2025-02-13 03:40:34

您可以使用AsyncMap在快照上迭代。

例如:

stream:_db.collection('users').snapshots().asyncMap((event) async {
          event.docs.map((userDoc) {
            var userData = userDoc.data() as Map<String, dynamic>;
            var referenceSnapshot = userData["someReference"].get();
          });
        })

You can iterate over the snapshots using asyncMap.

For example:

stream:_db.collection('users').snapshots().asyncMap((event) async {
          event.docs.map((userDoc) {
            var userData = userDoc.data() as Map<String, dynamic>;
            var referenceSnapshot = userData["someReference"].get();
          });
        })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文