English 中文(简体)
A. 更新工作管理的外部价值
原标题:Update external values with WorkManager

我有一个提供者,我有一份任务清单,那就是,在某个具体时间之后,我想执行一种方式,把任务从名单上删除。

class TaskProvider extends ChangeNotifier {
  final List<TaskModel> _tasks = [];
  List<TaskModel> get tasks => _tasks;
  final String _tasksKey = "tasks";

  Future<void> addTask({
    required String task,
    required DateTime scheduledTime,
    required TaskStatus status,
  }) async {
    scheduleTaskManager(task, scheduledTime);
    _tasks.add(
        TaskModel(task: task, limitDateTime: scheduledTime, status: status));
    notifyListeners();
  }

  Future<void> clearTasks() async {
    dev.log("Tasks deleted!");
    tasks.clear();
    notifyListeners();  
  }
}

通常,我可以通过进口和打电话清楚的Tasks(Task)来从《国际不动产合同》中做到这一点,但我无法与工作管理者一起使用,我的理解是,工作管理程序是分开的。

因此,我要问,如何利用工作管理改变外部价值?

这是退步。 派遣国:

@pragma(
     vm:entry-point ) // Mandatory if the App is obfuscated or using Flutter 3.1+
void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) {
    dev.log(
        "Native called background task: $task"); //simpleTask will be emitted here.
    dev.log("Time limit, clearing all tasks!");
    //Call clearTasks() or find a way to be able to modify the list of tasks of TaskProvider.
    return Future.value(true);
  });
}

虽然我最初尝试过说清楚的Tasks,但我可以看到任务标志被删除! 我没有看到这项任务被删除。

@pragma(
     vm:entry-point ) // Mandatory if the App is obfuscated or using Flutter 3.1+
void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) {
    dev.log(
        "Native called background task: $task"); //simpleTask will be emitted here.
    dev.log("Time limit, clearing all tasks!");
    TaskProvider taskProvider = TaskProvider();
    taskProvider.clearTasks();
    return Future.value(true);
  });
}

My idea is to modify various things in the task list, such as deleting them, updating the task status, etc. All this data comes from my provider.

问题回答

在您的提供者中,在这项职能中创建的任务并非相同。 由于这一职能必须达到顶级,可能有一种选择是建立一个静态的变更通知员,作为“工作管理者”向所有希望更新的人发出信息:

class WorkManagerNotifier extends ChangeNotifer {
  WorkManagerNotifier._();
  static final WorkManagerNotifier _instance = WorkManagerNotifier._();
  factory WorkManagerNotifier() => _instance;
  
  String? task;
  /// maybe an enum or bool to say is completed or failed  

  void updatedTask(String newTask) {
    task = newTask;
    notifiyListeners();
  }
}

@pragma(
     vm:entry-point ) // Mandatory if the App is obfuscated or using Flutter 3.1+
void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) {
    dev.log(
        "Native called background task: $task"); //simpleTask will be emitted here.
    dev.log("Time limit, clearing all tasks!");
    WorkManagerNotifier().updateTask(task); /// maybe you re interested in the data too?
    return Future.value(true);
  });
}

现在,你可以听从其他供应商的“工作管理者”,或在您的环境下使用:

/// main

ChangeNotifierValue(
  value: WorkManagerNotifier()
  child: MaterialApp...
)


/// where you create your TaskProvider
ChangeNotifierProxyProvider<WorkManagerNotifier, TaskProvider>(
  create: (_) => TaskProvider(...), ///
  update: (_, workManager, taskProvider) {
    if (workManager.task ==  task ) {
      taskProvider.clear();
    }
    return taskProvider;
  },
  child: ...
);

如今,当工兵揭发新任务时,有你所理解的名称任务或任务时,你可以向你们的提供者说明,将其用于你们想要的许多任务和投入数据。





相关问题
Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...

Moving the icon button to the right causes rendering issues

I am trying to align the icon button to the right of the Text field, I tried the row which made the icon button not centered vertically and now I am trying out the Stack widget. Here is my code - ...

热门标签