registerCallback static method
Registers a callback
to track the status and progress of a download
task.
callback
must be a top-level or static function of DownloadCallback
type which is called whenever the status or progress value of a download
task has been changed.
Your UI is rendered on the main isolate, while download events come from a
background isolate (in other words, code in callback
is run in the
background isolate), so you have to handle the communication between two
isolates.
Example:
ReceivePort _port = ReceivePort();
@override
void initState() {
super.initState();
IsolateNameServer.registerPortWithName(_port.sendPort, 'downloader_send_port');
_port.listen((dynamic data) {
String id = data[0];
DownloadTaskStatus status = DownloadTaskStatus(data[1]);
int progress = data[2];
setState((){ });
});
FlutterDownloader.registerCallback(downloadCallback);
}
static void downloadCallback(
String id,
int status,
int progress,
) {
final SendPort send = IsolateNameServer.lookupPortByName(
'downloader_send_port',
);
send.send([id, status, progress]);
}
Implementation
static Future<void> registerCallback(
DownloadCallback callback, {
int step = 10,
}) async {
assert(_initialized, 'plugin flutter_downloader is not initialized');
final callbackHandle = PluginUtilities.getCallbackHandle(callback);
assert(
callbackHandle != null,
'callback must be a top-level or static function',
);
assert(
0 <= step && step <= 100,
'step size is not in the inclusive <0;100> range',
);
await _channel.invokeMethod<void>(
'registerCallback',
<dynamic>[callbackHandle!.toRawHandle(), step],
);
}