Best way to signal a waiting function from another function?

Hi all. I am busy building an app that uses MQTT as a communication channel. The data comes in on a stream in an async manner. So I want to use the MQTT in a 2-way communication manner. I am starting off by building the login dialog for a user. So what will happen is that the app will build the login dialog that has the username and password on, when the user clicks on login the app will send a command on a topic that the server is listening on and the the app needs to wait and not remove the login dialog and show a spinner to the user to show that the app is waiting. The app will receive its answer on another topic it is listening on that the server will send the result on.

So my question is this. What is the best way to have the login dialog block / wait until we get a message on the topic that the server will send. This data will come in a stream that is not part of the login call. I do have access to the data via Provider. And how do I then send a message to the blocking dialog to continue. All the example I see is with await on the call to an API. The API will return on the same call. In my case the API will not return as it is running async. I also want an timeout on the blocking / waiting login dialog should we not get a response from the server.

So I need to wait (how do I wait? Do I sit in a while loop with sleeps? Is there a better way?) until I get the data back on a topic and then continue on getting the data or a timeout. I do have the MQTT all working, just need to figure out the best way to wait / block and then pass the data to that wait / blocking function to tell it to continue.

OK so I have it working. Just wondering if this is the best way.

After I send the login details to the server-side I will sit in a while loop looking at a string "appState.loginResult " that I get to via Provider. Inside the while loop, I will use an await Future to give the app time to do other things. I also have a counter that will add 1 after each wait and then will update the string I look at in Provider to cause a timeout as I break out of the loop. If the data comes in via MQTT the string is updated and then I break out of the while loop also and process the result. So this all seems to work 100% the way I want it, the question is is this the best way in Flutter or is there some other method I don’t know of?

//Update the result
                  appState.loginResult = 'X';
                  timeoutCounter = 0;

                  //We sit in the loop until the data changes from X
                  while (appState.loginResult == 'X') {
                    await Future.delayed(Duration(milliseconds: 200));
                    if (timeoutCounter >= 25) {
                      appState.loginResult = 'T';