且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Flutter 如何调度后台任务?

更新时间:2023-01-12 18:26:43

有一个 中型博文,解释了如何执行此操作.
但是,我们认为设置起来太复杂了,所以碰巧我们创建了一个 插件 来帮助您这个.

There is a Medium blogpost that explains how to do this.
However we thought it was way too complicated to set up so it just happens we created a plugin that aids you with this.

//Provide a top level function or static function.
//This function will be called by Android and will return the value you provided when you registered the task.
//See below
void callbackDispatcher() {
  Workmanager.executeTask((task) {
    print("Native echoed: $task");
    return Future.value(true);
  });
}

Workmanager.initialize(
    callbackDispatcher, //the top level function.
    isInDebugMode: true //If enabled it will post a notification whenever the job is running. Handy for debugging jobs
)

我们支持 Android 的 Workmanager 和 iOS performFetch

We support Android's Workmanager and iOS performFetch

目前它只适用于 Android 项目,但我们很快就会研究 iOS.
我会在可用时更新此答案.

For now it only works for Android project, but we are looking at iOS soon.
I'll update this answer when it is available.

我们现在也支持 iOS.它仍然是早期的 alpha,但请试一试.
我们也写了一篇免费的Medium 帖子.

We have iOS support now too. It is still early alpha, but give a go.
We wrote a complimentary Medium post too.