startForegroundService static method
serviceFunction needs to be self-contained i.e. all setup/init/etc. needs to be done entirely within serviceFunction since apparently due to how the implementation works callback is done within a new isolate, so memory is not shared (static variables will not have the same values, etc. etc.) communication of simple values between serviceFunction and the main app can be accomplished using setupIsolateCommunication & sendToPort
Implementation
static Future<void> startForegroundService(
[Function serviceFunction, bool holdWakeLock = false]) async {
//foreground service should only be started from the main isolate
if (!(await isBackgroundIsolate)) {
final setupHandle = PluginUtilities.getCallbackHandle(
_setupForegroundServiceCallbackChannel)
.toRawHandle();
//don't know why anyone would pass null, but w/e
final shouldHoldWakeLock = holdWakeLock ?? false;
await _invokeMainChannel(
"startForegroundService", <dynamic>[setupHandle, shouldHoldWakeLock]);
if (serviceFunction != null) {
setServiceFunction(serviceFunction);
}
} else {
throw _WrongIsolateException(await isBackgroundIsolate);
}
}