Flutter在后台运行Dart功能



我有一个dart函数someFunction,它需要几分钟的时间才能运行(它有大量的网络请求等待(。目前,当我运行该功能并将我的应用程序移动到后台时,似乎在离开应用程序之前排队的请求都被执行了,但随后该功能停止,当我返回应用程序时也不会恢复。我想做的是让功能在后台运行,直到完成,即使应用程序不在前台,如果应用程序不处于前台,也要在功能完成后发送通知。

我看过一些解决方案,比如后台获取插件和隔离,但除非我遗漏了什么,否则它们似乎只适用于本地代码或周期性的后台任务。我要找的是一个不管怎样都会运行一次并返回到主线程的任务,结果它可以对做出反应

函数的非Dart依赖项,如果这是相关的:

import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

你有没有尝试过这个包,带有WidgetsBindingObserver的workmanager(应用程序生命周期(?

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
//Do whatever you want in background
if (state == AppLifecycleState.paused ||
state == AppLifecycleState.detached) {
}
}

最新更新