registerHeadlessSyncBodyBuilder static method

Future<bool> registerHeadlessSyncBodyBuilder(
  1. Future<JsonMap> builder(
    1. SyncBodyContext context
    )
)

Registers a headless-compatible sync body builder.

The builder must be a top-level or static function (not a closure). Closures capture state that cannot be serialized for background execution.

Implementation

static Future<bool> registerHeadlessSyncBodyBuilder(
  Future<JsonMap> Function(SyncBodyContext context) builder,
) async {
  final dispatcherHandle =
      PluginUtilities.getCallbackHandle(_headlessSyncBodyDispatcher);
  final callbackHandle = PluginUtilities.getCallbackHandle(builder);

  if (dispatcherHandle == null || callbackHandle == null) {
    debugPrint(
        '[Locus] ERROR: Failed to register headless sync body builder.');
    debugPrint('[Locus]   Could not obtain callback handles.');
    debugPrint(
        '[Locus]   Ensure your builder is a top-level or static function, not a closure.');
    debugPrint('[Locus]   Example:');
    debugPrint('[Locus]     @pragma("vm:entry-point")');
    debugPrint(
        '[Locus]     Future<Map<String, dynamic>> buildSyncBody(SyncBodyContext ctx) async {');
    debugPrint(
        '[Locus]       return {"locations": ctx.locations.map((l) => l.toJson()).toList()};');
    debugPrint('[Locus]     }');
    return false;
  }

  final result = await LocusChannels.methods.invokeMethod(
    'registerHeadlessSyncBodyBuilder',
    {
      'dispatcher': dispatcherHandle.toRawHandle(),
      'callback': callbackHandle.toRawHandle(),
    },
  );

  _hasHeadlessBuilder = result == true;
  return _hasHeadlessBuilder;
}