fluttercommunity / fluttercommunity/flutter_downloader

LoadTasksWithRawQuery not working

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

Description

**LoadTasksWithRawQuery not giving proper value**

```
class DownloadHandler {
Future download({required String url}) async {
try {
if (kIsWeb) {
await JsProvider().downloadFile(url: url, name: url.split("/").last);
} else {
PopUpItems().toastMessage("Downloading ...", Colors.blueAccent);
_bindBackgroundIsolate();
await FlutterDownloader.registerCallback(downloadCallback, step: 1);
final taskId = await FlutterDownloader.enqueue(
url: url,
savedDir: await downloadPath(),
showNotification: true,
// show download progress in status bar (for Android)
openFileFromNotification: true,
// click on notification to open downloaded file (for Android),
saveInPublicStorage: true);

AppLog.i(taskId, tag: "TaskId");
}
} catch (e, stacktrace) {
AppLog.e(e.toString(), error: e, stackTrace: stacktrace);
}
}

Future downloadPath() async {
try {
String directoryPath;
if (Platform.isIOS) {
directoryPath = (await getApplicationDocumentsDirectory()).path ?? "";
} else {
directoryPath = "/storage/emulated/0/Download";

bool dirDownloadExists = await Directory(directoryPath).exists();
if (dirDownloadExists) {
directoryPath = "/storage/emulated/0/Download";
} else {
directoryPath = "/storage/emulated/0/Downloads";
}
}
bool dirDownloadExists = await Directory(directoryPath).exists();
if (!dirDownloadExists) {
await Directory(directoryPath).create();
}
return directoryPath;
} catch (e, stacktrace) {
AppLog.e(e.toString(), error: e, stackTrace: stacktrace);
}
return "";
}

@pragma('vm:entry-point')
static void downloadCallback(
String id,
int status,
int progress,
) async {
AppLog.i('task ($id) is in status ($status) and process ($progress)',
tag: 'Callback on background isolate');
IsolateNameServer.lookupPortByName('downloader_send_port')
?.send([id, status, progress]);
}

void _bindBackgroundIsolate() {
List? _tasks;
ReceivePort _port = ReceivePort();
final isSuccess = IsolateNameServer.registerPortWithName(
_port.sendPort,
'downloader_send_port',
);
if (!isSuccess) {
_unbindBackgroundIsolate();
_bindBackgroundIsolate();
return;
}
_port.listen((dynamic data) async {
final taskId = (data as List)[0] as String;
final status = DownloadTaskStatus.fromInt(data[1] as int);
final progress = data[2] as int;
// var v = await FlutterDownloader.loadTasks();
AppLog.i('task ($taskId) is in status ($status) and process ($progress)',
tag: 'Callback on UI isolate:');

if (progress == 100) {
String query = "SELECT * FROM task WHERE task_id='$taskId'";
List? tasks =
await FlutterDownloader.loadTasksWithRawQuery(query: query);

await Permission.storage.request();
await Permission.manageExternalStorage.request();
await Permission.accessMediaLocation.request();
await OpenFilex.open(
"${tasks?.first.savedDir ?? ""}/${tasks?.first.filename ?? ""}");
}
});
}

void _unbindBackgroundIsolate() {
IsolateNameServer.removePortNameMapping('downloader_send_port');
}
}

class TaskInfo {
TaskInfo({this.name, this.link});

final String? name;
final String? link;

String? taskId;
int? progress = 0;
DownloadTaskStatus? status = DownloadTaskStatus.undefined;
}

```

**Expected behavior**

**Give proper value.**

Device:- IOS, Flutter version 3.19.6, plugin version 1.11.6

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the FlutterDownloader.loadTasksWithRawQuery entry point and reproduce the shown SELECT query on iOS with Flutter 3.19.6 and plugin version 1.11.6. Check the returned DownloadTask values for the completed task; done means the query returns the expected task data so the subsequent savedDir and filename lookup works.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
databases, mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.