Flutter Download File Example [extra Quality] | POPULAR | Blueprint |
If you want users to see downloaded files in the "Files" app, add UIFileSharingEnabled and LSSupportsOpeningDocumentsInPlace to your Info.plist .
import 'dart:io'; import 'package:dio/dio.dart'; import 'package:path_provider/path_provider.dart'; class FileDownloadService { static Future downloadFile({ required String url, required String fileName, required Function(double) onProgress, }) async { Dio dio = Dio(); try { // Get the correct directory for the platform Directory dir = await getApplicationDocumentsDirectory(); String savePath = "${dir.path}/$fileName"; await dio.download( url, savePath, onReceiveProgress: (received, total) { if (total != -1) { double progress = received / total; onProgress(progress); } }, ); print("File saved to: $savePath"); } catch (e) { print("Download error: $e"); } } } Use code with caution. 🖥️ UI Example: Progress Bar flutter download file example
class DownloadScreen extends StatefulWidget { @override _DownloadScreenState createState() => _DownloadScreenState(); } class _DownloadScreenState extends State { double _progress = 0; bool _isDownloading = false; void _startDownload() async { setState(() { _isDownloading = true; _progress = 0; }); await FileDownloadService.downloadFile( url: "https://example.com", fileName: "manual.pdf", onProgress: (p) { setState(() => _progress = p); }, ); setState(() => _isDownloading = false); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("Flutter File Download")), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ if (_isDownloading) ...[ LinearProgressIndicator(value: _progress), SizedBox(height: 20), Text("${(_progress * 100).toStringAsFixed(0)}%"), ], ElevatedButton( onPressed: _isDownloading ? null : _startDownload, child: Text("Download PDF"), ), ], ), ), ); } } Use code with caution. 💡 Best Practices If you want users to see downloaded files
Finds the correct folders (Documents, Temporary) on both iOS and Android. 📂 Implementation Steps 1. Request Permissions null : _startDownload, child: Text("Download PDF"), ), ],