从字段中检索Firestore自动生成的文档ID

时间:2020-10-26 19:54:53

标签: flutter dart google-cloud-firestore

我目前正在使用“餐厅配送”应用,正在使用Cloud Fire Store存储当前订单。我有一个名为订单的子集合,并且文件是自动生成的,并且我有多个字段,例如名称,电子邮件,地址和状态。订购时的状态为“等待中”。订购后,当我将其连接到流时,该订单将显示在管理屏幕中。如果管理员接受订单,我想将订单状态更新为“已接受”,但要这样做,我需要文档ID,但这是自动生成的。是否有任何方法可以检索此自动生成的ID。如果是,请帮助我。如果没有人建议我,可以使用一种方法来检索适当订单的适当文档ID。

谢谢!

 class CurrentOrderStream extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    DateTime now = new DateTime.now();
    String date = "${now.day}-${now.month}-${now.year}";
    return StreamBuilder<QuerySnapshot>(
      stream: firestore
          .collection('orders')
          .doc(date)
          .collection('orders')
          .where('status', isEqualTo: 'waiting')
          .snapshots(),
      builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (!snapshot.hasData) {
          return Center(
            child: CircularProgressIndicator(
              backgroundColor: Colors.lightBlueAccent,
            ),
          );
        }

        List<CurrentOrderCard> currentOrderCardWidget = [];
        final messages = snapshot.data.docs;
        for (var i in messages) {
          print(i['name']);
          print(i['email']);
          print(i['order']);
          print(i['mobileNumber']);
          print(i['count']);
          print(i['type']);
          print(i['total']);
          print(i['deliveryAddress']);
          print(i['url']);
          String _order = i['order'].toString().replaceAll('[', '');
          String _count = i['count'].toString().replaceAll('[', '');
          String _type = i['type'].toString().replaceAll('[', '');
          String _url = i['url'].toString().replaceAll('[', '');
          _url = _url.toString().replaceAll(']', '');
          _count = _count.toString().replaceAll(']', '');
          _type = _type.toString().replaceAll(']', '');
          _order = _order.toString().replaceAll(']', '');

          print(_count);
          print(_order);
          print(_type);
          print(_url);

          currentOrderCardWidget.add(CurrentOrderCard(
            name: i['name'],
            address: i['deliveryAddress'],
            email: i['email'],
            order: _order,
            mobileNumber: i['mobileNumber'],
          ));
        }
        return ListView(
          children: currentOrderCardWidget,
        );
      },
    );
  }
}

class CurrentOrderCard extends StatelessWidget {
  final name;
  final mobileNumber;
  final address;
  final order;
  final email;
  final count;
  final type;
  final total;
  final url;
  CurrentOrderCard(
      {this.name,
      this.order,
      this.mobileNumber,
      this.address,
      this.email,
      this.count,
      this.total,
      this.type,
      this.url});

  @override
  Widget build(BuildContext context) {
    return Material(
      elevation: 20,
      color: Colors.transparent,
      child: Container(
        padding: EdgeInsets.all(15),
        margin: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
        width: MediaQuery.of(context).size.width,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10), color: containerColor),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text('Order',
                style: GoogleFonts.montserrat(
                    fontSize: 18, fontWeight: FontWeight.bold)),
            SizedBox(
              height: 5,
            ),
            Text(order, style: GoogleFonts.montserrat(fontSize: 12)),
            SizedBox(
              height: 10,
            ),
            Text('Delivery Address',
                style: GoogleFonts.montserrat(
                    fontSize: 18, fontWeight: FontWeight.bold)),
            SizedBox(
              height: 5,
            ),
            Text(address, style: GoogleFonts.montserrat(fontSize: 12)),
            SizedBox(
              height: 15,
            ),
            Divider(
              thickness: 2,
            ),
            SizedBox(
              height: 5,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                ElevatedButton(
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.resolveWith(
                        (states) => Colors.red),
                  ),
                  onPressed: () {},
                  child: Row(
                    children: [
                      Text('DECLINE'),
                      SizedBox(
                        width: 5,
                      ),
                      Icon(
                        Icons.cancel,
                        size: 20,
                      )
                    ],
                  ),
                ),
                ElevatedButton(
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.resolveWith(
                        (states) => Colors.green),
                  ),
                  onPressed: () {},
                  child: Row(
                    children: [
                      Text('ACCEPT'),
                      SizedBox(
                        width: 5,
                      ),
                      Icon(
                        Icons.check,
                        size: 20,
                      )
                    ],
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

0 个答案:

没有答案
相关问题