initDispatcher static method
Initialize Dispatcher to relay events occurring in the foreground service to taskCallback. It must always be called from a top-level function, otherwise foreground tasks will not work.
Implementation
static void initDispatcher(TaskCallback taskCallback, {DestroyCallback? onDestroy}) {
// Create a method channel to communicate with the platform.
const _backgroundChannel = MethodChannel('flutter_foreground_task/background');
// Binding the framework to the flutter engine.
WidgetsFlutterBinding.ensureInitialized();
// Set the method call handler for the background channel.
_backgroundChannel.setMethodCallHandler((call) async {
final timestamp = DateTime.now();
if (call.method == 'event') {
await taskCallback(timestamp, _lookupPort());
} else if (call.method == 'destroy') {
if (onDestroy != null)
await onDestroy(timestamp);
}
});
// Initializes the plug-in background channel and starts a foreground task.
_backgroundChannel.invokeMethod('initialize');
}