API Reference
API Reference

The callbackDispatcher() registers a background method call handler for processing events when your app is not in the foreground. This function should be set as the entry point for background isolates.

Example:

@pragma('vm:entry-point')
void callbackDispatcher() {
  const MethodChannel _backgroundChannel =
      MethodChannel('moca_flutter_background');
  WidgetsFlutterBinding.ensureInitialized();

  _backgroundChannel.setMethodCallHandler((MethodCall call) async {
    try {
      final args = call.arguments;
      final CallbackHandle handle = CallbackHandle.fromRawHandle(args[0]);
      final Function? callback = PluginUtilities.getCallbackFromHandle(handle);
      final Map res = args[1];
      callback?.call(res);
    } catch (e, stackTrace) {
      developer.log("Error in background callback: " + e.toString(),
          name: 'Moca', stackTrace: stackTrace);
    }
  });
}