Please help me, i'm new to Firebase and i have a message error when i'm trying to create a collection from an existing collection ID.
The conversation is created and the ChatRoom also but i have the message appearing and disappearing after around 10 secondes, it seems as the second function doesn't wait the first one
Here's the structure :
Here's the message error :
The following assertion was thrown building chatPage(dirty, state: _chatPageState#e5c15): a document path must be a non-empty string 'package:cloud_firestore/src/collection_reference.dart': Failed assertion: line 59 pos 14: 'path.isNotEmpty'
Here's my code :
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();
}
Welcome to SO! I'm having trouble understanding the code fully. It seems like some may be missing, specifically a complete definition of _chatPageState()
. As a tip for future questions, you'll generally find it easier to get responses if you can strip the code back to a simplified example where possible.
My first line of thought is: does it work if conversationId
is a value inside the _chatPageState
instead of being a static property of chatPage
?
My second thought is: does it work if you chain the two async functions like this?
getConversationID().then((id) {
chatPage.conversationId = id;
createChatRoomMessageAuto();
});
If the latter works, you probably may not even need to store conversationId
at all as you might be able to pass it straight through to the createChatRoomMessageAuto
function like
getConversationID().then((id) {
createChatRoomMessageAuto(id);
});