0

我正在构建一个应用程序,它收集从 Firebase 下达的所有订单和订单详细信息。我必须得到两件事

  • 用户登录后我使用单例方法保存的联系人号码中的沙龙详细信息
  • 来自 CustID 的客户详细信息

现在发生的是,在调试期间我创建了这个按钮,按下它会从数据库中获取沙龙详细信息。但是现在,只有当我获取详细信息时

  1. 先点击按钮
  2. 热重启应用

只有这样,streambuilder 才能获取数据这是导致问题的代码片段:

Future<void> getSalonFromContact(String saloonContact) async {
    await for (var docs in firestore.collection('Saloon').snapshots()) {
      //  final loop = snap.data!.docs;
      for (var variable in docs.docs) {
        if (variable.get(FieldPath(['Contact'])) == saloonContact) {
          aadhar = variable.get(FieldPath(['Aadhar']));

          getOrdersList(aadhar);
        }
      }
    }
  }

  Future<void> getOrdersList(String aadhar) async {
    ordersList.clear();
    await for (var docs in firestore
        .collection('orders')
        .where('SalonID', isEqualTo: aadhar)
        .snapshots()) {
      for (var variable in docs.docs) {
        if (variable.get('SalonID') == aadhar) {
          ordersList.add(variable.data());

          print('My orderlist is $ordersList');
        } else {
          continue;
        }
      }
    }
  }

  Future<void> getCustomerDetails(String custID) async {
    await for (var docs in firestore
        .collection('Customers')
        .where('Customer_Uid', isEqualTo: custID)
        .snapshots()) {
      //  final loop = snap.data!.docs;
      for (var variable in docs.docs) {
        print(variable.data());
        if (variable.get(FieldPath(['Customer_Uid'])) == custID) {
          customerDetails.add(variable.data());
          print('My customer details are ${customerDetails}');
        }
      }
    }
  }

  @override
  void didChangeDependencies() async {
    await getSalonFromContact(contactNumber);
    for (int i = 0; i < ordersList.length; i++) {
      await getCustomerDetails(ordersList[i]['CustomerID']);
    }
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
  }

这些代码用于查找详细信息。

这是我的 StreamBuilder 代码:

 StreamBuilder<QuerySnapshot>(
                      stream: FirebaseFirestore.instance
                          .collection('orders')
                          .where('SalonID', isEqualTo: aadhar)
                          .snapshots(),
                      builder: (context, snapshot) {
                        didChangeDependencies();
                        if (snapshot.connectionState ==
                            ConnectionState.waiting) {
                          return Text('Loading...');
                        } else {
                          List<AppointmentCard> listitems = [];

                          return ListView(
                            physics: NeverScrollableScrollPhysics(),
                            shrinkWrap: true,
                            children: snapshot.data!.docs
                                .asMap()
                                .map((index, DocumentSnapshot document) {
                                  getCustomerDetails(document['CustomerID']);
                                  return MapEntry(
                                    index,
                                    AppointmentCard(
                                      isCompleted: document['Status'],
                                      name: customerDetails[index]['Name'],
                                      contact: customerDetails[index]
                                          ['Contact'],
                                      services: Flexible(
                                        child: ListView.builder(
                                            shrinkWrap: true,
                                            padding: const EdgeInsets.all(8),
                                            itemCount:
                                                document['Requested_Service']
                                                    .length,
                                            itemBuilder: (BuildContext context,
                                                int index) {
                                              return Text(
                                                  document['Requested_Service']
                                                      [index]['name']);
                                            }),
//                                child: Text(
//                                  //    "Text",
//                                  " ${ordersList[i]['Requested_Service']} ",
////                                      .join(' '),
//
//                                  softWrap: true,
//                                ),
                                      ),
                                    ),
                                  );
                                })
                                .values
                                .toList(),
                          );
                        }
                      }),

知道出了什么问题以及如何在没有按钮和热重启的情况下获取数据?

4

1 回答 1

0

getCustomerDetails(document['CustomerID']);在 MapEntry 之前使用 in ,它是一个异步函数。它可能会在 MapEntry 构建后返回。在将正在更新的变量放入 getCustomerDetails 函数之前,您必须等待 getCustomerDetails 函数。

于 2021-08-27T12:49:00.403 回答