There are a few things to consider. Saving a file differs on various devices. In the case of PDF files, I assume that you want them to be easily accessible to the user. To make it happen, it can't be stored in the application's files - it would require a lot of digging to find the file by the user himself. It also requires several permissions, some of which are configured directly in platform-specific files and some have to be requested from the user in runtime. Permission Handler package comes in handy for this job. So, here is how to implement it: import 'dart:io'; import 'package:flutter/services.dart'; import 'package:path_provider/path_provider.dart'; import 'package:permission_handler/permission_handler.dart';iOS Future<void> saveFile(String fileName) async { var file = File(''); // Platform.isIOS comes from dart:io if (Platform.isIOS) { final dir = await getApplicationDocumentsDirectory(); file = File('${dir.path}/$fileName'); }To make it work on iOS you have to add 2 keys to ios/Runner/Info.plist file. <key>LSSupportsOpeningDocumentsInPlace</key> <true/> <key>UIFileSharingEnabled</key> <true/>Android if (Platform.isAndroid) { var status = await Permission.storage.status; if (status != PermissionStatus.granted) { status = await Permission.storage.request(); } if (status.isGranted) { const downloadsFolderPath = '/storage/emulated/0/Download/'; Directory dir = Directory(downloadsFolderPath); file = File('${dir.path}/$fileName'); } }For Android, on the other hand, you'll have to do a few more things. At first, it is required to set proper compileSdkVersion in android/app/build.gradle file. As I'm writing this - it should be set to 33. Also 2 lines are needed in android/app/src/main/AndroidManifest.xml, right below manifest <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>And of course, as it can be seen in the code - we also need to request a permission from a user. Writing to the file // load file that you want to save on user's phone // it can be loaded from whenever you want, e.g. some API. final byteData = await rootBundle.load('assets/$fileName'); try { await file.writeAsBytes(byteData.buffer .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes)); } on FileSystemException catch (err) { // handle error } }File Saver package saves files to the application's data, so it won't be so easy to find for the user. It is also published by an unverified uploader so I'd definitely keep it in mind. (责任编辑:) |