fix selectFile()

This commit is contained in:
deltamaya
2024-10-28 17:20:53 +08:00
parent abc3623026
commit f0c7bfd52e

View File

@@ -160,11 +160,15 @@ class DirectoryPicker {
Future<file_selector.XFile?> selectFile({required List<String> ext}) async { Future<file_selector.XFile?> selectFile({required List<String> ext}) async {
file_selector.XTypeGroup typeGroup = file_selector.XTypeGroup( file_selector.XTypeGroup typeGroup = file_selector.XTypeGroup(
label: 'files', label: 'files',
extensions: ext, extensions: App.isMacOS || App.isIOS ? null : ext,
); );
final file_selector.XFile? file = await file_selector.openFile( final file_selector.XFile? file = await file_selector.openFile(
acceptedTypeGroups: <file_selector.XTypeGroup>[typeGroup], acceptedTypeGroups: <file_selector.XTypeGroup>[typeGroup],
); );
if (file == null) return null;
if (!ext.contains(file?.path.split(".").last)) {
return null;
}
return file; return file;
} }
@@ -175,18 +179,18 @@ Future<String?> selectDirectory() async {
Future<void> saveFile( Future<void> saveFile(
{Uint8List? data, required String filename, File? file}) async { {Uint8List? data, required String filename, File? file}) async {
if(data == null && file == null) { if (data == null && file == null) {
throw Exception("data and file cannot be null at the same time"); throw Exception("data and file cannot be null at the same time");
} }
if(data != null) { if (data != null) {
var cache = FilePath.join(App.cachePath, filename); var cache = FilePath.join(App.cachePath, filename);
if(File(cache).existsSync()) { if (File(cache).existsSync()) {
File(cache).deleteSync(); File(cache).deleteSync();
} }
await File(cache).writeAsBytes(data); await File(cache).writeAsBytes(data);
file = File(cache); file = File(cache);
} }
if(App.isMobile) { if (App.isMobile) {
final params = SaveFileDialogParams(sourceFilePath: file!.path); final params = SaveFileDialogParams(sourceFilePath: file!.path);
await FlutterFileDialog.saveFile(params: params); await FlutterFileDialog.saveFile(params: params);
} else { } else {