What i am trying to do is run Flutter Downloader package to download a list of files at a particular time every day, i have used WorkManager to schedule and run the task, but the task does not run, i currently set it to check the scheduled download each 1 hour but it does not run.
void callbackDispatcher() {
Workmanager().executeTask((taskName, inputData) async {
switch (taskName) {
case fetchBackground:
//check the time and download the movie
// if (DateTime.now().hour == 23) {
//TODO change to the correct call to sharedPrefrences
ScheduledDownload(); //then run the download task.
// }
break;
}
return Future.value(true);
});
}
void main() async {
//initialize WorkManager
await Workmanager().initialize(callbackDispatcher, isInDebugMode: true);
//register the periodic background task
await Workmanager().registerPeriodicTask(
"1",
fetchBackground,
frequency: const Duration(hours: 1),
);
}
And this is the main class, the Downloading using Flutter Downloader background task that i am want to run
class DownloadCallBackClass {
@pragma('vm:entry-point')
static void downloadCallBack(
String id, DownloadTaskStatus status, int progress) {
final SendPort? send =
IsolateNameServer.lookupPortByName('downloader_send_port');
send!.send([id, status, progress]);
}
}
class ScheduledDownload {
List<DownloadTask>? _downloadTasks;
final ReceivePort _receivePort = ReceivePort();
void _loadTasks() async {
_downloadTasks = await FlutterDownloader.loadTasks();
if (_downloadTasks!.length > 0) {
print ("the number of download task is greater than 1");
_downloadTasks!.forEach((element) async {
if (element.status == DownloadTaskStatus.paused) {
await FlutterDownloader.resume(taskId: element.taskId);
} else if (element.status == DownloadTaskStatus.failed) {
String _url = element.url;
await FlutterDownloader.remove(taskId: element.taskId);
await FlutterDownloader.enqueue(
url: _url, savedDir: '/storage/emulated/0/Download');
}
});
}
print ("the number of task is less than 1");
}
void _initializeFlutterDownloader() async {
await FlutterDownloader.initialize(
debug: true,
ignoreSsl: true,
).then((value) {
IsolateNameServer.registerPortWithName(
_receivePort.sendPort, 'downloader_send_port');
_receivePort.listen((dynamic data) {
String id = data[0];
DownloadTaskStatus status = data[1];
int progress = data[2];
// setState(() {
// _progress = progress;
// _status = status;
// });
});
FlutterDownloader.registerCallback(
DownloadCallBackClass.downloadCallBack);
_loadTasks();
});
}
ScheduledDownload() {
//initialize flutter downloader
print("in scheduled download");
_initializeFlutterDownloader();
}
}