DEV Community

Cover image for Flutter: Background services

Flutter: Background services

protium on June 17, 2019

So, it's time for me to actually try Flutter. I've watched the last dev conferences and I'm amazed of what Flutter is able to do and the future Flu...
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. :)

Collapse
 
icesofty profile image
Florian

Thanks! Really helpful

Collapse
 
ariefannur profile image
Arief Maffrudin A N

greate example. very helpful. How about implementation in iOS ?

Collapse
 
protium profile image
protium

Thanks! Well I got a Mac few weeks ago, so now I can finally give it a try. Will update soon :)

Collapse
 
mummoorthi profile image
Mummoorthi • Edited

please implementation in IOS for background service...

Collapse
 
roboticscm profile image
roboticscm

Thanks Brian Mayo,
It's very nice app.
Could you please make the app run as services?
The music is still off after 60 minutes even though Rest app closed

Collapse
 
protium profile image
protium

Hey, thanks!
The app is using a service to pause the music. Maybe you found a bug, please feel free to submit an issue with some details on how to replicate the issue
github.com/protium-dev/rest

Collapse
 
giorgiobertolotti profile image
Giorgio Bertolotti

This is very useful, thanks!

Collapse
 
yafetissac profile image
yafetissac

already solved at here icetutor.com/question/how-do-i-run...

Collapse
 
tahaan profile image
Johan Hartzenberg

Where does each snippet of code go into the project?

What goes into the Service project, what goes into the app? Or is it just one project?