From 1f3b7b5ba3d850ee043f0e6570aa5e0253975339 Mon Sep 17 00:00:00 2001 From: Eiichiro Adachi Date: Mon, 27 Apr 2020 19:49:04 +0900 Subject: [PATCH 1/2] Handle updateProgress for Android. --- lib/src/downloader.dart | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/src/downloader.dart b/lib/src/downloader.dart index 267cd295..0fa8e62d 100644 --- a/lib/src/downloader.dart +++ b/lib/src/downloader.dart @@ -386,6 +386,22 @@ class FlutterDownloader { static registerCallback(DownloadCallback callback) { assert(_initialized, 'FlutterDownloader.initialize() must be called first'); + if (callback != null) { + // remove previous setting + _channel.setMethodCallHandler(null); + _channel.setMethodCallHandler((MethodCall call) async { + if (call.method == 'updateProgress') { + String id = call.arguments['task_id']; + int status = call.arguments['status']; + int process = call.arguments['progress']; + callback(id, DownloadTaskStatus(status), process); + } + return null; + }); + } else { + _channel.setMethodCallHandler(null); + } + final callbackHandle = PluginUtilities.getCallbackHandle(callback); assert(callbackHandle != null, 'callback must be a top-level or a static function'); From e99d85ddf50d8f5ffc920a6ce47953d48846fd49 Mon Sep 17 00:00:00 2001 From: Eiichiro Adachi Date: Tue, 7 Sep 2021 13:37:39 +0900 Subject: [PATCH 2/2] update README and example --- README.md | 2 ++ example/lib/main.dart | 1 + 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index ed4ed365..43f4cc31 100644 --- a/README.md +++ b/README.md @@ -273,6 +273,7 @@ void initState() { @override void dispose() { + FlutterDownloader.registerCallback(null); IsolateNameServer.removePortNameMapping('downloader_send_port'); super.dispose(); } @@ -284,6 +285,7 @@ static void downloadCallback(String id, DownloadTaskStatus status, int progress) ```` +- Note: set `callback` as `null` to remove listener. You should clean up callback to prevent from leaking references. #### Load all tasks: diff --git a/example/lib/main.dart b/example/lib/main.dart index d6b32106..a8cc011d 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -129,6 +129,7 @@ class _MyHomePageState extends State { @override void dispose() { + FlutterDownloader.registerCallback(null); _unbindBackgroundIsolate(); super.dispose(); }