0

When I call my function "randomChat" it somehow returns an empty value, which should not happen with my code.

randomChat Function:

    Future<String> randomChat(String userId) async {
  String roomId = await connectRoom(userId);
  return roomId;
}

connectRoom Function:

   Future<String> connectRoom(String userId) async {
  List<String> connectedUsers = await getChattingWith(userId);
  late String roomId;

  // Find all the rooms where userId2 is empty
  FirebaseFirestore.instance
      .collection('rooms')
      .where('userId2', isEqualTo: "")
      .where("userId1", isNotEqualTo: userId)
      .get()
      .then((snapshot) async {
    // If no empty room is found, create room
    if (snapshot.docs.isEmpty) {
      print("call empty");
      String roomId = await createRoom(userId);
      return roomId;
    }

//        // If an empty room which is not connected to userId (part of connectedUsers) is found, connect to it
    for (int i = 0; i < snapshot.docs.length; i++) {
      var data = snapshot.docs[i].data();
      Room room = Room.fromJson(data);
// check if the empty room found is not connected to userId
      bool found = connectedUsers.contains(room.userId1);

      if (!found) {
        FirebaseFirestore.instance
            .collection('rooms')
            .doc(room.id)
            .update({"userId2": userId});
        print("call not found = $roomId");
        return roomId = room.id;
      } else {
        
        roomId = await createRoom(userId);
        print("call found = $roomId");
        return roomId;
      }
    }
  });
  print("call nothing = $roomId");
  return roomId;
}

And here I am calling the function and printing the value (which is empty, but should not be empty. It should return my roomId):

   onPressed: () async {
                String roomId = await randomChat(uid);
                print(roomId);
     
              },

Create Room function (where I also correctly get the roomId printed):

    Future<String> createRoom(String userId) async {
  DocumentReference docRef =
      await FirebaseFirestore.instance.collection("rooms").add({
    "userId1": userId,
    "userId2": "",
    "created_at": DateTime.now().toString(),
  });
  await docRef.update({"roomId": docRef.id});
  print("create room: ${docRef.id}");
  return docRef.id;
}

1 Answer 1

1

In your connectRoom, you need to await for FirebaseFirestore result:

await FirebaseFirestore.instance
      .collection('rooms')
      .where('userId2', isEqualTo: "")
      .where("userId1", isNotEqualTo: userId)
      .get()
      .then((snapshot) async {
  ...
}

also you define new String roomId in every if statement inside then function, just pass that value to your main roomId which is define as late String roomId.

Sign up to request clarification or add additional context in comments.

10 Comments

you said if you print roomId exactly before return roomId; inside randomChat that would show its value?
could you add the print code to your question so I see where do you have it?
Just added it. Thank you for being so helpful!
@cipano I just updated you question and add some print, could you run that and tell me the result?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.