Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] File path slash was hardcoded and caused issue on Windows when providing a file path to FileSaver.saveFile #129

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions lib/src/platform_handler/platform_handler_all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> saveFileForAndroid(FileModel fileModel) async {
try {
directory =
await _channel.invokeMethod<String>(_saveFile, fileModel.toMap()) ??
'';
directory = await _channel.invokeMethod<String>(_saveFile, fileModel.toMap()) ?? '';
return directory;
} catch (e) {
rethrow;
Expand All @@ -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();
Expand Down Expand Up @@ -70,8 +68,7 @@ class PlatformHandlerAll extends PlatformHandler {
if (Platform.isAndroid || Platform.isIOS || Platform.isMacOS) {
path = await _channel.invokeMethod<String>(_saveAs, fileModel.toMap());
} else if (Platform.isWindows) {
final Int64List? bytes =
await _channel.invokeMethod<Int64List?>('saveAs', fileModel.toMap());
final Int64List? bytes = await _channel.invokeMethod<Int64List?>('saveAs', fileModel.toMap());
path = bytes == null ? null : String.fromCharCodes(bytes);
} else {
throw UnimplementedError('Unimplemented Error');
Expand Down
23 changes: 13 additions & 10 deletions lib/src/utils/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -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) {
Expand All @@ -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('.')) {
Expand All @@ -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 {
Expand Down