From 85dba080cde0fea12dd465692b20ef82aee45d1c Mon Sep 17 00:00:00 2001 From: Miguel Ruivo Date: Sat, 21 Nov 2020 21:26:32 +0000 Subject: [PATCH 1/4] Adds null safety support (#510) --- example/lib/src/file_picker_demo.dart | 2 +- example/pubspec.yaml | 3 ++ lib/src/file_picker.dart | 16 +++++----- lib/src/file_picker_io.dart | 34 ++++++++++----------- lib/src/file_picker_result.dart | 2 +- lib/src/file_picker_web.dart | 44 +++++++++++++-------------- lib/src/platform_file.dart | 12 ++++---- 7 files changed, 57 insertions(+), 56 deletions(-) diff --git a/example/lib/src/file_picker_demo.dart b/example/lib/src/file_picker_demo.dart index 308705d..dd61119 100644 --- a/example/lib/src/file_picker_demo.dart +++ b/example/lib/src/file_picker_demo.dart @@ -50,7 +50,7 @@ class _FilePickerDemoState extends State { void _clearCachedFiles() { FilePicker.platform.clearTemporaryFiles().then((result) { - _scaffoldKey.currentState.showSnackBar( + ScaffoldMessenger.of(context).showSnackBar( SnackBar( backgroundColor: result ? Colors.green : Colors.red, content: Text((result diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 5cc47c5..3de146c 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -2,6 +2,9 @@ name: file_picker_example description: An example of how to use the file_picker plugin. version: 1.0.0+1 +environment: + sdk: ">=2.8.1 <3.0.0" + dependencies: flutter: sdk: flutter diff --git a/lib/src/file_picker.dart b/lib/src/file_picker.dart index 2ab97a1..d7bd8c8 100644 --- a/lib/src/file_picker.dart +++ b/lib/src/file_picker.dart @@ -60,14 +60,14 @@ abstract class FilePicker extends PlatformInterface { /// with useful information regarding the picked [List]. /// /// Returns [null] if aborted. - Future pickFiles({ + Future pickFiles({ FileType type = FileType.any, - List allowedExtensions, - Function(FilePickerStatus) onFileLoading, - bool allowCompression, + List? allowedExtensions, + Function(FilePickerStatus)? onFileLoading, + bool? allowCompression, bool allowMultiple = false, - bool withData, - bool withReadStream, + bool? withData, + bool? withReadStream, }) async => throw UnimplementedError('pickFiles() has not been implemented.'); @@ -78,13 +78,13 @@ abstract class FilePicker extends PlatformInterface { /// of it whenever needed. However, this will force the cleanup if you want to manage those on your own. /// /// Returns [true] if the files were removed with success, [false] otherwise. - Future clearTemporaryFiles() async => throw UnimplementedError( + Future clearTemporaryFiles() async => throw UnimplementedError( 'clearTemporaryFiles() has not been implemented.'); /// Selects a directory and returns its absolute path. /// /// On Android, this requires to be running on SDK 21 or above, else won't work. /// Returns [null] if folder path couldn't be resolved. - Future getDirectoryPath() async => + Future getDirectoryPath() async => throw UnimplementedError('getDirectoryPath() has not been implemented.'); } diff --git a/lib/src/file_picker_io.dart b/lib/src/file_picker_io.dart index acc9b1f..d168611 100644 --- a/lib/src/file_picker_io.dart +++ b/lib/src/file_picker_io.dart @@ -21,17 +21,17 @@ const EventChannel _eventChannel = /// An implementation of [FilePicker] that uses method channels. class FilePickerIO extends FilePicker { static const String _tag = 'MethodChannelFilePicker'; - static StreamSubscription _eventSubscription; + static StreamSubscription? _eventSubscription; @override - Future pickFiles({ + Future pickFiles({ FileType type = FileType.any, - List allowedExtensions, - Function(FilePickerStatus) onFileLoading, - bool allowCompression = true, + List? allowedExtensions, + Function(FilePickerStatus)? onFileLoading, + bool? allowCompression = true, bool allowMultiple = false, - bool withData = false, - bool withReadStream = false, + bool? withData = false, + bool? withReadStream = false, }) => _getPath( type, @@ -44,11 +44,11 @@ class FilePickerIO extends FilePicker { ); @override - Future clearTemporaryFiles() async => + Future clearTemporaryFiles() async => _channel.invokeMethod('clear'); @override - Future getDirectoryPath() async { + Future getDirectoryPath() async { try { return await _channel.invokeMethod('dir', {}); } on PlatformException catch (ex) { @@ -60,14 +60,14 @@ class FilePickerIO extends FilePicker { return null; } - Future _getPath( + Future _getPath( FileType fileType, bool allowMultipleSelection, - bool allowCompression, - List allowedExtensions, - Function(FilePickerStatus) onFileLoading, - bool withData, - bool withReadStream, + bool? allowCompression, + List? allowedExtensions, + Function(FilePickerStatus)? onFileLoading, + bool? withData, + bool? withReadStream, ) async { final String type = describeEnum(fileType); if (type != 'custom' && (allowedExtensions?.isNotEmpty ?? false)) { @@ -85,7 +85,7 @@ class FilePickerIO extends FilePicker { ); } - final List result = await _channel.invokeListMethod(type, { + final List? result = await _channel.invokeListMethod(type, { 'allowMultipleSelection': allowMultipleSelection, 'allowedExtensions': allowedExtensions, 'allowCompression': allowCompression, @@ -102,7 +102,7 @@ class FilePickerIO extends FilePicker { platformFiles.add( PlatformFile.fromMap( platformFileMap, - readStream: withReadStream + readStream: withReadStream! ? File(platformFileMap['path']).openRead() : null, ), diff --git a/lib/src/file_picker_result.dart b/lib/src/file_picker_result.dart index f6ae337..ecf531d 100644 --- a/lib/src/file_picker_result.dart +++ b/lib/src/file_picker_result.dart @@ -27,5 +27,5 @@ class FilePickerResult { .toList(); /// A `List` containing all names from picked files with its extensions. - List get names => files.map((file) => file.name).toList(); + List get names => files.map((file) => file.name).toList(); } diff --git a/lib/src/file_picker_web.dart b/lib/src/file_picker_web.dart index 6b919ba..463b1ca 100644 --- a/lib/src/file_picker_web.dart +++ b/lib/src/file_picker_web.dart @@ -9,7 +9,7 @@ import 'file_picker_result.dart'; import 'platform_file.dart'; class FilePickerWeb extends FilePicker { - Element _target; + late Element _target; final String _kFilePickerInputsDomId = '__file_picker_web-file-input'; final int _readStreamChunkSize = 1000 * 1000; // 1 MB @@ -26,12 +26,12 @@ class FilePickerWeb extends FilePicker { /// Initializes a DOM container where we can host input elements. Element _ensureInitialized(String id) { - Element target = querySelector('#$id'); + Element? target = querySelector('#$id'); if (target == null) { final Element targetElement = Element.tag('flt-file-picker-inputs') ..id = id; - querySelector('body').children.add(targetElement); + querySelector('body')!.children.add(targetElement); target = targetElement; } return target; @@ -40,18 +40,18 @@ class FilePickerWeb extends FilePicker { @override Future pickFiles({ FileType type = FileType.any, - List allowedExtensions, + List? allowedExtensions, bool allowMultiple = false, - Function(FilePickerStatus) onFileLoading, - bool allowCompression, - bool withData = true, - bool withReadStream = false, + Function(FilePickerStatus)? onFileLoading, + bool? allowCompression, + bool? withData = true, + bool? withReadStream = false, }) async { final Completer> filesCompleter = Completer>(); String accept = _fileType(type, allowedExtensions); - InputElement uploadInput = FileUploadInputElement(); + InputElement uploadInput = FileUploadInputElement() as InputElement; uploadInput.draggable = true; uploadInput.multiple = allowMultiple; uploadInput.accept = accept; @@ -63,14 +63,14 @@ class FilePickerWeb extends FilePicker { } changeEventTriggered = true; - final List files = uploadInput.files; + final List files = uploadInput.files!; final List pickedFiles = []; void addPickedFile( File file, - Uint8List bytes, - String path, - Stream> readStream, + Uint8List? bytes, + String? path, + Stream>? readStream, ) { pickedFiles.add(PlatformFile( name: file.name, @@ -86,15 +86,15 @@ class FilePickerWeb extends FilePicker { } files.forEach((File file) { - if (withReadStream) { + if (withReadStream!) { addPickedFile(file, null, null, _openFileReadStream(file)); return; } - if (!withData) { + if (!withData!) { final FileReader reader = FileReader(); reader.onLoadEnd.listen((e) { - addPickedFile(file, null, reader.result, null); + addPickedFile(file, null, reader.result as String?, null); }); reader.readAsDataUrl(file); return; @@ -102,7 +102,7 @@ class FilePickerWeb extends FilePicker { final FileReader reader = FileReader(); reader.onLoadEnd.listen((e) { - addPickedFile(file, reader.result, null, null); + addPickedFile(file, reader.result as Uint8List?, null, null); }); reader.readAsArrayBuffer(file); }); @@ -119,7 +119,7 @@ class FilePickerWeb extends FilePicker { return FilePickerResult(await filesCompleter.future); } - static String _fileType(FileType type, List allowedExtensions) { + static String _fileType(FileType type, List? allowedExtensions) { switch (type) { case FileType.any: return ''; @@ -137,11 +137,9 @@ class FilePickerWeb extends FilePicker { return 'video/*|image/*'; case FileType.custom: - return allowedExtensions.fold( - '', (prev, next) => '${prev.isEmpty ? '' : '$prev,'} .$next'); - break; + return allowedExtensions! + .fold('', (prev, next) => '${prev.isEmpty ? '' : '$prev,'} .$next'); } - return ''; } Stream> _openFileReadStream(File file) async* { @@ -155,7 +153,7 @@ class FilePickerWeb extends FilePicker { final blob = file.slice(start, end); reader.readAsArrayBuffer(blob); await reader.onLoad.first; - yield reader.result; + yield reader.result as List; start += _readStreamChunkSize; } } diff --git a/lib/src/platform_file.dart b/lib/src/platform_file.dart index 43bd682..f2adccc 100644 --- a/lib/src/platform_file.dart +++ b/lib/src/platform_file.dart @@ -21,21 +21,21 @@ class PlatformFile { /// ``` /// final File myFile = File(platformFile.path); /// ``` - final String path; + final String? path; /// File name including its extension. - final String name; + final String? name; /// Byte data for this file. Particurlarly useful if you want to manipulate its data /// or easily upload to somewhere else. - final Uint8List bytes; + final Uint8List? bytes; /// File content as stream - final Stream> readStream; + final Stream>? readStream; /// The file size in bytes. - final int size; + final int? size; /// File extension for this file. - String get extension => name?.split('.')?.last; + String? get extension => name?.split('.').last; } From cae0765e3e9646bf1b4cca3fb2959caa57cee999 Mon Sep 17 00:00:00 2001 From: Miguel Ruivo Date: Fri, 8 Jan 2021 22:55:38 +0000 Subject: [PATCH 2/4] Bumps version --- CHANGELOG.md | 12 +++++++----- example/pubspec.yaml | 2 +- pubspec.yaml | 8 ++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cf9dc9..88cc80c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +## 3.0.0-nullsafety.2 +Adds null safety support (pre-release) ([#510](https://github.com/miguelpruivo/flutter_file_picker/issues/510)). ## 2.1.7 ### iOS - Fixes an issue where a crash could happen when picking a lot of media files in low memory devices ([#606](https://github.com/miguelpruivo/flutter_file_picker/issues/606)). @@ -17,13 +19,13 @@ iOS & Android: Updates `size` property from `PlatformFile` to be in bytes instea iOS: Fixes iOS ViewController which is nil when UIWindow.rootViewController have changed. ([#525](https://github.com/miguelpruivo/flutter_file_picker/issues/525)). Thank you @devcxm. ## 2.1.3 -Android: Updates file name handling method. ([#487](https://github.com/miguelpruivo/flutter_file_picker/issues/487)). +Android: Updates file name handling method. ([#487](https://github.com/miguelpruivo/flutter_file_picker/issues/487)) ## 2.1.2 -Desktop (Go): Fixed desktop plugin implementation. Thank you @DenchikBY. +Desktop (Go): Fixed desktop plugin implementation. Thank you @DenchikBY. ([#382](https://github.com/miguelpruivo/flutter_file_picker/issues/382#issuecomment-744055654)) ## 2.1.1 -iOS: Fixes an issue that could result in a crash when selecting a media item twice. ([#518](https://github.com/miguelpruivo/flutter_file_picker/issues/518)). +iOS: Fixes an issue that could result in a crash when selecting a media item twice. ([#518](https://github.com/miguelpruivo/flutter_file_picker/issues/518)) ## 2.1.0 Adds `withReadStream` that allows bigger files to be streamed read into a `Stream>`. Thanks @redsolver. @@ -34,8 +36,8 @@ Updates `extension` helper getter to use the `name` property instead of `path`, ## 2.0.12 Android: -- Fixes an issue that could result in some files not being properly retrieved due to special characters on their names. ([#472](https://github.com/miguelpruivo/flutter_file_picker/issues/472)). -- Fixes a NPE that could happen with some devices. ([#482](https://github.com/miguelpruivo/flutter_file_picker/issues/482)). +- Fixes an issue that could result in some files not being properly retrieved due to special characters on their names. ([#472](https://github.com/miguelpruivo/flutter_file_picker/issues/472)) +- Fixes a NPE that could happen with some devices. ([#482](https://github.com/miguelpruivo/flutter_file_picker/issues/482)) ## 2.0.11 diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 3de146c..c3edf91 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -3,7 +3,7 @@ description: An example of how to use the file_picker plugin. version: 1.0.0+1 environment: - sdk: ">=2.8.1 <3.0.0" + sdk: '>=2.10.0 <3.0.0' dependencies: flutter: diff --git a/pubspec.yaml b/pubspec.yaml index b068c18..6be6b1d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: file_picker description: A package that allows you to use a native file explorer to pick single or multiple absolute file paths, with extension filtering support. homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker -version: 2.1.7 +version: 3.0.0-nullsafety.2 dependencies: flutter: @@ -9,11 +9,11 @@ dependencies: flutter_web_plugins: sdk: flutter - flutter_plugin_android_lifecycle: ^1.0.6 - plugin_platform_interface: ^1.0.1 + flutter_plugin_android_lifecycle: ^2.0.0-nullsafety + plugin_platform_interface: ^1.1.0-nullsafety.1 environment: - sdk: ">=2.0.0 <3.0.0" + sdk: ">=2.12.0-29.10.beta <3.0.0" flutter: ">=1.10.0 <2.0.0" flutter: From e8075e3f9679dbe38436c8e474da68917d330584 Mon Sep 17 00:00:00 2001 From: Miguel Ruivo Date: Wed, 3 Mar 2021 21:35:49 +0000 Subject: [PATCH 3/4] Updates plugin_platform_interface --- CHANGELOG.md | 2 +- lib/src/file_picker_result.dart | 2 +- pubspec.yaml | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88cc80c..e8a3b8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## 3.0.0-nullsafety.2 +## 3.0.0-nullsafety.4 Adds null safety support (pre-release) ([#510](https://github.com/miguelpruivo/flutter_file_picker/issues/510)). ## 2.1.7 ### iOS diff --git a/lib/src/file_picker_result.dart b/lib/src/file_picker_result.dart index ecf531d..2c50356 100644 --- a/lib/src/file_picker_result.dart +++ b/lib/src/file_picker_result.dart @@ -19,7 +19,7 @@ class FilePickerResult { /// original files (which can be accessed through its URI property). /// /// Only available on IO. Throws `UnsupportedError` on Web. - List get paths => files + List get paths => files .map((file) => kIsWeb ? throw UnsupportedError( 'Picking paths is unsupported on Web. Please, use bytes property instead.') diff --git a/pubspec.yaml b/pubspec.yaml index 6be6b1d..ecdb15e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: file_picker description: A package that allows you to use a native file explorer to pick single or multiple absolute file paths, with extension filtering support. homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker -version: 3.0.0-nullsafety.2 +version: 3.0.0-nullsafety.4 dependencies: flutter: @@ -10,10 +10,10 @@ dependencies: sdk: flutter flutter_plugin_android_lifecycle: ^2.0.0-nullsafety - plugin_platform_interface: ^1.1.0-nullsafety.1 + plugin_platform_interface: ^2.0.0 environment: - sdk: ">=2.12.0-29.10.beta <3.0.0" + sdk: ">=2.12.0 <3.0.0" flutter: ">=1.10.0 <2.0.0" flutter: From 80c90a972a692efc88fc3b951a8751accc61ac2c Mon Sep 17 00:00:00 2001 From: Miguel Ruivo Date: Thu, 4 Mar 2021 18:01:11 +0000 Subject: [PATCH 4/4] Promote null safety --- CHANGELOG.md | 4 ++-- example/lib/generated_plugin_registrant.dart | 9 ++++----- pubspec.yaml | 8 ++++---- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8a3b8a..ae1b3a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ -## 3.0.0-nullsafety.4 -Adds null safety support (pre-release) ([#510](https://github.com/miguelpruivo/flutter_file_picker/issues/510)). +## 3.0.0 +Adds null safety support ([#510](https://github.com/miguelpruivo/flutter_file_picker/issues/510)). ## 2.1.7 ### iOS - Fixes an issue where a crash could happen when picking a lot of media files in low memory devices ([#606](https://github.com/miguelpruivo/flutter_file_picker/issues/606)). diff --git a/example/lib/generated_plugin_registrant.dart b/example/lib/generated_plugin_registrant.dart index daa72e8..0b73330 100644 --- a/example/lib/generated_plugin_registrant.dart +++ b/example/lib/generated_plugin_registrant.dart @@ -2,15 +2,14 @@ // Generated file. Do not edit. // -// ignore: unused_import -import 'dart:ui'; +// ignore_for_file: lines_longer_than_80_chars import 'package:file_picker/src/file_picker_web.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; // ignore: public_member_api_docs -void registerPlugins(PluginRegistry registry) { - FilePickerWeb.registerWith(registry.registrarFor(FilePickerWeb)); - registry.registerMessageHandler(); +void registerPlugins(Registrar registrar) { + FilePickerWeb.registerWith(registrar); + registrar.registerMessageHandler(); } diff --git a/pubspec.yaml b/pubspec.yaml index ecdb15e..18f6d24 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: file_picker description: A package that allows you to use a native file explorer to pick single or multiple absolute file paths, with extension filtering support. homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker -version: 3.0.0-nullsafety.4 +version: 3.0.0 dependencies: flutter: @@ -9,13 +9,13 @@ dependencies: flutter_web_plugins: sdk: flutter - flutter_plugin_android_lifecycle: ^2.0.0-nullsafety + flutter_plugin_android_lifecycle: ^2.0.0 plugin_platform_interface: ^2.0.0 environment: sdk: ">=2.12.0 <3.0.0" - flutter: ">=1.10.0 <2.0.0" - + flutter: ">=1.10.0" + flutter: plugin: platforms: