Created
January 17, 2022 20:10
-
-
Save MCarlomagno/4ec3e59f8a42e3716cd192bc97553fd0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
import 'package:dio/dio.dart'; | |
import 'package:open_file/open_file.dart'; | |
import 'package:path_provider/path_provider.dart'; | |
import 'package:permission_handler/permission_handler.dart'; | |
... | |
class MobileDownloadService implements DownloadService { | |
@override | |
Future<void> download({required String url}) async { | |
// requests permission for downloading the file | |
bool hasPermission = await _requestWritePermission(); | |
if (!hasPermission) return; | |
// gets the directory where we will download the file. | |
var dir = await getApplicationDocumentsDirectory(); | |
// You should put the name you want for the file here. | |
// Take in account the extension. | |
String fileName = 'myFile'; | |
// downloads the file | |
Dio dio = Dio(); | |
await dio.download(url, "${dir.path}/$fileName"); | |
// opens the file | |
OpenFile.open("${dir.path}/$fileName", type: 'application/pdf'); | |
} | |
// requests storage permission | |
Future<bool> _requestWritePermission() async { | |
await Permission.storage.request(); | |
return await Permission.storage.request().isGranted; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment