executeTask method

  1. @override
Future<BackgroundTaskInfo> executeTask(
  1. BackgroundTask taskCallback, {
  2. PlatformArguments? args,
  3. String? tag,
})
override

Implementation

@override
Future<BackgroundTaskInfo> executeTask(BackgroundTask taskCallback, {PlatformArguments args, String? tag}) async {
  try {
    if (!_startedInitialization) {
      throw Exception("BackgroundTaskManager initialization not initiated. Please call BackgroundTaskManager.singleton.init()");
    }

    if (!initCompletable.isCompleted) await initCompletable.future;
    if (!isInitialized) throw Exception("BackgroundTaskManager is not initialized.");
    final result = await _methodChannel.invokeMethod("executeTask",
        {"tag": tag, "callbackHandle": _callbackDispatcher.toRawHandle, "taskHandle": taskCallback.toRawHandle, "args": args?.toRawMap()});
    BackgroundTaskInfo? task;
    if (result is Map) {
      task = BackgroundTaskInfo.fromMap(result
        ..addEntries([
          MapEntry("handle", taskCallback.toRawHandle),
          MapEntry("args", args?.toRawMap()),
        ]));
      await cache.put(task);
    }
    debugPrint("executeTask success $result");
    if (task == null) throw Exception("Task is Null. Something went wrong. Platform result : $result, task : $task");
    return task;
  } on Exception catch (e) {
    debugPrint("executeTask Exception $e");
    rethrow;
  } on Error catch (e) {
    debugPrint("executeTask Error $e");
    rethrow;
  }
}