enqueueUniqueTask method

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

Implementation

@override
Future<BackgroundTaskInfo> enqueueUniqueTask(BackgroundTask taskCallback, String uniqueWorkName, {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("enqueueUniqueTask", {
      "tag": tag,
      "callbackHandle": _callbackDispatcher.toRawHandle,
      "taskHandle": taskCallback.toRawHandle,
      "uniqueWorkName": uniqueWorkName,
      "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;
  }
}