From a4b1543cc6cefa454f79494e531997e997c733b7 Mon Sep 17 00:00:00 2001 From: Nikolai Henriksen Date: Tue, 12 Nov 2024 14:12:02 +0100 Subject: [PATCH] [FIX] File path slash was hardcoded and caused issue on Windows when providing a file path to FileSaver.saveFile --- .../platform_handler_all.dart | 13 ++++------- lib/src/utils/helpers.dart | 23 +++++++++++-------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/src/platform_handler/platform_handler_all.dart b/lib/src/platform_handler/platform_handler_all.dart index 50f94bc..2ab7de8 100644 --- a/lib/src/platform_handler/platform_handler_all.dart +++ b/lib/src/platform_handler/platform_handler_all.dart @@ -20,14 +20,11 @@ class PlatformHandlerAll extends PlatformHandler { 'Something went wrong, please report the issue https://www.github.com/incrediblezayed/file_saver/issues'; late String directory = _somethingWentWrong; - final String _issueLink = - 'https://www.github.com/incrediblezayed/file_saver/issues'; + final String _issueLink = 'https://www.github.com/incrediblezayed/file_saver/issues'; Future saveFileForAndroid(FileModel fileModel) async { try { - directory = - await _channel.invokeMethod(_saveFile, fileModel.toMap()) ?? - ''; + directory = await _channel.invokeMethod(_saveFile, fileModel.toMap()) ?? ''; return directory; } catch (e) { rethrow; @@ -41,7 +38,8 @@ class PlatformHandlerAll extends PlatformHandler { log('The path was found null or empty, please report the issue at $_issueLink'); throw Exception('The path was found null or empty'); } else { - String filePath = '$path/${fileModel.name}${fileModel.ext}'; + final slash = Helpers.getFilePathSlash(); + String filePath = '$path$slash${fileModel.name}${fileModel.ext}'; final File file = File(filePath); await file.writeAsBytes(fileModel.bytes); bool exist = await file.exists(); @@ -70,8 +68,7 @@ class PlatformHandlerAll extends PlatformHandler { if (Platform.isAndroid || Platform.isIOS || Platform.isMacOS) { path = await _channel.invokeMethod(_saveAs, fileModel.toMap()); } else if (Platform.isWindows) { - final Int64List? bytes = - await _channel.invokeMethod('saveAs', fileModel.toMap()); + final Int64List? bytes = await _channel.invokeMethod('saveAs', fileModel.toMap()); path = bytes == null ? null : String.fromCharCodes(bytes); } else { throw UnimplementedError('Unimplemented Error'); diff --git a/lib/src/utils/helpers.dart b/lib/src/utils/helpers.dart index 32e9110..62f06ca 100644 --- a/lib/src/utils/helpers.dart +++ b/lib/src/utils/helpers.dart @@ -6,10 +6,8 @@ import 'package:dio/dio.dart'; import 'package:file_saver/src/models/link_details.dart'; import 'package:flutter/foundation.dart'; import 'package:path_provider/path_provider.dart' as path_provider; -import 'package:path_provider_linux/path_provider_linux.dart' - as path_provider_linux; -import 'package:path_provider_windows/path_provider_windows.dart' - as path_provder_windows; +import 'package:path_provider_linux/path_provider_linux.dart' as path_provider_linux; +import 'package:path_provider_windows/path_provider_windows.dart' as path_provder_windows; ///Helper Class for serveral utility methods /// @@ -74,12 +72,10 @@ class Helpers { } else if (Platform.isMacOS) { path = (await path_provider.getDownloadsDirectory())?.path; } else if (Platform.isWindows) { - path_provder_windows.PathProviderWindows pathWindows = - path_provder_windows.PathProviderWindows(); + path_provder_windows.PathProviderWindows pathWindows = path_provder_windows.PathProviderWindows(); path = await pathWindows.getDownloadsPath(); } else if (Platform.isLinux) { - path_provider_linux.PathProviderLinux pathLinux = - path_provider_linux.PathProviderLinux(); + path_provider_linux.PathProviderLinux pathLinux = path_provider_linux.PathProviderLinux(); path = await pathLinux.getDownloadsPath(); } } on Exception catch (e) { @@ -90,6 +86,14 @@ class Helpers { return path; } + static String getFilePathSlash() { + if (Platform.isWindows) { + return '\\'; + } else { + return '/'; + } + } + ///This method is used to format the extension as per the requirement static String getExtension({required String extension}) { if (extension.contains('.')) { @@ -110,8 +114,7 @@ class Helpers { Dio? dioClient, Uint8List Function(dynamic data)? transformDioResponse, }) async { - assert(filePath != null || link != null || file != null, - 'Either filePath or link or file must be provided'); + assert(filePath != null || link != null || file != null, 'Either filePath or link or file must be provided'); if (filePath != null) { return _getBytesFromPath(filePath); } else {