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();
await _invokeMainChannel(
"startForegroundService", <dynamic>[setupHandle, holdWakeLock]);
// if (serviceFunction != null) {
// setServiceFunction(serviceFunction);
// }
} else {
throw WrongIsolateException(await isBackgroundIsolate);
}
}