DEV Community

Discussion on: Flutter: Background services

Collapse
 
ravenblackx profile image
ravenblackx

A small suggestion for how you could avoid the tangle of keepResult:

  1. Make onMethodCall an async function.

    public void onMethodCall(MethodCall call, MethodChannel.Result result) async {
    
  2. In onMethodCall's "connect" branch,

    await connectToService();
    result.success(null);
    
  3. Make connectToService return a Future that gets completed by the onServiceConnected callback. (Likely using a Completer.)

Not going into too much more detail because I see in the repository that all the code is completely different already, this comment is more to try to provide other passers-by with some tools to avoid the same structural issues. :)

Collapse
 
ravenblackx profile image
ravenblackx

Oh, my mistake, I was missing that that entire section isn't Flutter/Dart code at all! Found it further into the repository. Turns out nearly all of the post's code is Java!

The same premise still applies, but now it all makes more sense to me as well. :)