Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart Completer.complete() results never resolve

Tags:

flutter

dart

I have a function that returns a future which depends on the result of a callback to resolve:

   Future connectSocket(String email, String password, {Function onConnectCallback}) async {
    var completer = new Completer();
    print("Connecting...");
    var query = getQueryString(email, password);
    socketIO = await SocketIOManager().createInstance(SocketOptions(localDomainWindows, query: query));

    socketIO.on("loginError", (data) {
      print("Login err");
      _connected = false;
      connectedCallback();
      completer.complete(false);
    });

    socketIO.onConnect((data) {
      print("***CONNECTED***");
      _connected = true;
      completer.complete(true);
      connectedCallback();
    });
    socketIO.connect();
    return completer.future;
  }

I can see ***CONNECTED*** printed to the console, and my socket server acknowledges the connection, but the function await-ing the resolution never resumes, it just hangs.

    socketConnection.connectSocket(_email, _password)
    .then((success) {
     print("SUCCESS") // never gets printed
}
like image 725
Cameron Sima Avatar asked Dec 03 '25 18:12

Cameron Sima


1 Answers

The only possible explanation for this is that some code in the callback is blocking your program from continuing because Completer.complete should otherwise always make the future complete.
If it is blocked, however, the event loop will never be able to call your code.

As a bool assignment should never be blocking (_connected = true;), the only part of your function that could be halting your program is connectedCallack();. If you remove or fix it, you should see your future complete.

like image 154
creativecreatorormaybenot Avatar answered Dec 06 '25 17:12

creativecreatorormaybenot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!