fluttercommunity / fluttercommunity/flutter_downloader

downloadCallback is not being called after pop back. Callback is being overridden by new one ......

Open
#573 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
940
Forks
561
Avg merge
1h 18m
Merged PRs (30d)
1

Description

Guys, help me

I have 2 pages: Page A and Page B
On both pages i have files downloading feature

**Page A has code:**

```
final ReceivePort _receivePort = ReceivePort();

/// Message listener from isolate
void _messageListener(dynamic message) {
final taskId = message[0] as String;
final status = message[1] as DownloadTaskStatus;
final progress = message[2] as int;
print("Documents list: progress - $progress");
setState(() { });
if (status == DownloadTaskStatus.running) {
widget.showLoader(true);
}
else if (status == DownloadTaskStatus.complete) {
_openFile(taskId);
widget.showLoader(false);
} else {
widget.showLoader(false);
}
}

/// Callback for downloading
static void downloadCallback(
String id, DownloadTaskStatus status, int progress) {
final SendPort sendPort =
IsolateNameServer.lookupPortByName("download_isolate");
sendPort.send([id, status, progress]);
}

/// Open downloaded file
Future _openFile(String taskId) async {
final tasks = await FlutterDownloader.loadTasks();
final task = tasks.firstWhere((task) => task.taskId == taskId);
final path = task.savedDir.replaceAll("/(null)", "");
final filePath = '$path/${task.filename}';
final result = await OpenFile.open(filePath);
print(result.message);
print(result.type);
}

void _downloadFile(int documentId, String url) async {
final bloc = context.read();
final token = await bloc.getToken();
if (token != null && token.isNotEmpty) {
_downloadDocument(token, url);
}
}

Future _downloadDocument(String token, String url) async {
final state = await Permission.storage.request();
if (state.isGranted || Platform.isIOS) {
final externalDir = Platform.isAndroid
? await getExternalStorageDirectory()
: await getApplicationDocumentsDirectory();

FlutterDownloader.enqueue(
headers: {'Authorization': 'Bearer $token'},
url: url.toString(),
savedDir: externalDir.path,
);

}
}

@override
void initState() {
super.initState();
IsolateNameServer.registerPortWithName(
_receivePort.sendPort, "comments_download_isolate");
_receivePort.listen((message) => _messageListener(message));
FlutterDownloader.registerCallback(downloadCallback);
}

@override
void dispose() {
IsolateNameServer.removePortNameMapping("comments_download_isolate");
super.dispose();
}
```

**Page B has quite 100% the same code , but with another port_name_id(!)**

**So i have 2 download callbacks on 2 pages, both alive. Documentation never said its incorrect.**

Issue Flow is:
1) App Starts, shows Page A, downloading working fine
2) I press next > go to Page B (pushNamed)
3) on Page B downloading works fine
4) then i press back button, go to Page A again
5) and on Page A downloading not working. > DownloadCallback breakpoint got stopped on Page B (not on Page A). > DownloadCallback seems to being overridden by new callback on Page B.

How to Solve this issue? Can i set specific callbacks to specific page?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.