Flutter - Creating Document Collection from an existing Document Collection Error : path.isNotEmpty

Flutter - Creating Document Collection from an existing Document Collection Error : path.isNotEmpty

请帮助我,我是 Firebase 的新手,当我尝试从现有集合 ID 创建集合时出现消息错误。

对话已创建,聊天室也已创建,但我的消息在大约 10 秒后出现和消失,似乎第二个功能不等待第一个功能

结构如下:

这是消息错误:

构建聊天页面时引发了以下断言(脏,状态:_chatPageState#e5c15): 文档路径必须是非空字符串 'package:cloud_firestore/src/collection_reference.dart': 断言失败:第 59 行第 14 行:'path.isNotEmpty'

这是我的代码:

class chatPage extends StatefulWidget {
  @override
  _chatPageState createState() => _chatPageState();

  static String conversationId = "";

}

  final CollectionReference conversationRef =
      FirebaseFirestore.instance.collection("Conversations");

Future<String> getConversationID() async {
    //Initialize Conversations collection
    final QuerySnapshot snapshot = await conversationRef.get();
    String convId = "";

    snapshot.docs.forEach((DocumentSnapshot doc) {
      if (doc["NumTel_Parent"] == OTPScreen.numTelParent &&
          doc["Rendu"] == "Null") {
        _isConv = true;
        print("Conversation already Exist");
        convId = doc.id;
      }
    });
    if (_isConv == false) {
      print("Adding conversation ...");
      await bambiDB().addConversation(OTPScreen.numTelParent, "Null", "Null");

      snapshot.docs.forEach((DocumentSnapshot doc1) {
        if (doc1["NumTel_Parent"] == OTPScreen.numTelParent &&
            doc1["Rendu"] == "Null") {
          _isConv = true;
          print("Conversation created");
          convId = doc1.id;
        }
      });
    }
    return convId;
  }

  createChatRoomMessageAuto() async {
      final CollectionReference chatRoomRef = FirebaseFirestore.instance
          .collection("ChatRoom")
          .doc(chatPage.conversationId)
          .collection("ChatRoomParent");

      final QuerySnapshot snapshot = await chatRoomRef.get();

      if (snapshot.docs.length == 0) {
        chatRoomRef.add({
          "Message": messageAuto1,
          "DateMessage": DateTime.now(),
          "Who": "Doctor"
        }).then((_) {
          print("auto1 created");
        }).catchError((_) {
          print("an error occured");
        });

        chatRoomRef.add({
          "Message": messageAuto2,
          "DateMessage": DateTime.now(),
          "Who": "Doctor"
        }).then((_) {
          print("auto2 created");
        }).catchError((_) {
          print("an error occured");
        });

        chatRoomRef.add({
          "Message": messageAuto3,
          "DateMessage": DateTime.now(),
          "Who": "Doctor"
        }).then((_) {
          print("auto3 created");
        }).catchError((_) {
          print("an error occured");
        });

        chatRoomRef.add({
          "Message": messageAuto4,
          "DateMessage": DateTime.now(),
          "Who": "Doctor"
        }).then((_) {
          print("auto4 created");
        }).catchError((_) {
          print("an error occured");
        });
      } else {
        print("Auto already created.");
      }
  }

 void creatingChatRoom() async {
    chatPage.conversationId = "";

    chatPage.conversationId = await getConversationID();

    createChatRoomMessageAuto();

    setState(() {});
  }

 @override
  void initState() {
    // TODO: implement initState
    super.initState();
    creatingChatRoom();
  }

欢迎来到 SO!我无法完全理解代码。似乎缺少一些内容,特别是 _chatPageState() 的完整定义。作为未来问题的提示,如果您可以尽可能将代码剥离回简化示例,您通常会发现更容易获得答复。

我的第一个想法是:如果 conversationId_chatPageState 中的一个值而不是 chatPage 的静态 属性 是否有效?

我的第二个想法是:如果你像这样链接两个异步函数是否有效?

getConversationID().then((id) {
    chatPage.conversationId = id;
    createChatRoomMessageAuto();
});

如果后者有效,您可能甚至根本不需要存储 conversationId,因为您可以将它直接传递给 createChatRoomMessageAuto 函数,例如

getConversationID().then((id) {
    createChatRoomMessageAuto(id);
});

Me playing with a simplified version if it helps

感谢您的帮助,它现在可以正常工作了,确实我没有把所有的代码都放上,对不起,我不太清楚。