startForegroundService static method

Future<void> startForegroundService(
  1. [Function serviceFunction,
  2. bool holdWakeLock = false]

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


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(

    //don't know why anyone would pass null, but w/e
    final shouldHoldWakeLock = holdWakeLock ?? false;

    await _invokeMainChannel(
        "startForegroundService", <dynamic>[setupHandle, shouldHoldWakeLock]);

    if (serviceFunction != null) {
  } else {
    throw _WrongIsolateException(await isBackgroundIsolate);