Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ void initState() {

@override
void dispose() {
FlutterDownloader.registerCallback(null);
IsolateNameServer.removePortNameMapping('downloader_send_port');
super.dispose();
}
Expand All @@ -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:

Expand Down
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class _MyHomePageState extends State<MyHomePage> {

@override
void dispose() {
FlutterDownloader.registerCallback(null);
_unbindBackgroundIsolate();
super.dispose();
}
Expand Down
27 changes: 21 additions & 6 deletions lib/src/downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,28 @@ class FlutterDownloader {
///
/// {@end-tool}
///
static registerCallback(DownloadCallback callback) {
static registerCallback(DownloadCallback? callback) {
assert(_initialized, 'FlutterDownloader.initialize() must be called first');

final callbackHandle = PluginUtilities.getCallbackHandle(callback)!;
assert(callbackHandle != null,
'callback must be a top-level or a static function');
_channel.invokeMethod(
'registerCallback', <dynamic>[callbackHandle.toRawHandle()]);
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);
}
});

final callbackHandle = PluginUtilities.getCallbackHandle(callback)!;
assert(callbackHandle != null,
'callback must be a top-level or a static function');
_channel.invokeMethod(
'registerCallback', <dynamic>[callbackHandle.toRawHandle()]);
} else {
_channel.setMethodCallHandler(null);
}
}
}