From 6ef2634d5ab79a2ff25c84c738b30d2ac8cd68e8 Mon Sep 17 00:00:00 2001 From: Juanjo Tugores Date: Fri, 22 Jan 2021 15:55:32 -0600 Subject: [PATCH] Add logger --- .../example/lib/open_text_page.dart | 2 +- .../file_selector/lib/file_selector.dart | 21 +++++++++++++++++-- .../file_selector/file_selector/pubspec.yaml | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/file_selector/file_selector/example/lib/open_text_page.dart b/packages/file_selector/file_selector/example/lib/open_text_page.dart index 299d0e2dc21a..ac8bdbb45909 100644 --- a/packages/file_selector/file_selector/example/lib/open_text_page.dart +++ b/packages/file_selector/file_selector/example/lib/open_text_page.dart @@ -8,7 +8,7 @@ class OpenTextPage extends StatelessWidget { label: 'text', extensions: ['txt', 'json'], ); - final XFile file = await openFile(acceptedTypeGroups: [typeGroup]); + final XFile file = await openFile(); final String fileName = file.name; final String fileContent = await file.readAsString(); diff --git a/packages/file_selector/file_selector/lib/file_selector.dart b/packages/file_selector/file_selector/lib/file_selector.dart index 080eac4460ac..9e981cc04550 100644 --- a/packages/file_selector/file_selector/lib/file_selector.dart +++ b/packages/file_selector/file_selector/lib/file_selector.dart @@ -3,18 +3,20 @@ // found in the LICENSE file. import 'dart:async'; - +import 'package:logger/logger.dart'; import 'package:file_selector_platform_interface/file_selector_platform_interface.dart'; - export 'package:file_selector_platform_interface/file_selector_platform_interface.dart' show XFile, XTypeGroup; +final _logger = Logger(); + /// Open file dialog for loading files and return a file path Future openFile({ List acceptedTypeGroups, String initialDirectory, String confirmButtonText, }) { + _verifyExtensions(acceptedTypeGroups); return FileSelectorPlatform.instance.openFile( acceptedTypeGroups: acceptedTypeGroups, initialDirectory: initialDirectory, @@ -27,6 +29,7 @@ Future> openFiles({ String initialDirectory, String confirmButtonText, }) { + _verifyExtensions(acceptedTypeGroups); return FileSelectorPlatform.instance.openFiles( acceptedTypeGroups: acceptedTypeGroups, initialDirectory: initialDirectory, @@ -40,6 +43,7 @@ Future getSavePath({ String suggestedName, String confirmButtonText, }) async { + _verifyExtensions(acceptedTypeGroups); return FileSelectorPlatform.instance.getSavePath( acceptedTypeGroups: acceptedTypeGroups, initialDirectory: initialDirectory, @@ -55,3 +59,16 @@ Future getDirectoryPath({ return FileSelectorPlatform.instance.getDirectoryPath( initialDirectory: initialDirectory, confirmButtonText: confirmButtonText); } + +void _verifyExtensions(List acceptedTypeGroups) { + acceptedTypeGroups?.asMap()?.forEach((i, acceptedTypeGroup) { + acceptedTypeGroup.extensions?.asMap()?.forEach((j, ext) { + if (ext.startsWith('.')) { + _logger.w( + 'acceptedTypeGroups[${i}].extensions[${j}] with value "${ext}" is invalid.' + ' Remove the leading dot.', + ); + } + }); + }); +} diff --git a/packages/file_selector/file_selector/pubspec.yaml b/packages/file_selector/file_selector/pubspec.yaml index a55b7f4e06e7..fd34ff305576 100644 --- a/packages/file_selector/file_selector/pubspec.yaml +++ b/packages/file_selector/file_selector/pubspec.yaml @@ -7,6 +7,7 @@ dependencies: flutter: sdk: flutter file_selector_platform_interface: ^1.0.0 + logger: ^0.9.4 dev_dependencies: flutter_test: