From d08ee6b453b16ed20e3b9395a4ac1e8b0d3e800d Mon Sep 17 00:00:00 2001 From: vito-go <56462631+vito-go@users.noreply.github.com> Date: Sat, 20 Apr 2024 23:10:26 +0800 Subject: [PATCH 1/8] fix: make a type discrimination of the reader.result. When built in the html web-renderer, an error may occur: TypeError: Instance of 'NativeByteBuffer': type 'NativeByteBuffer' is not a subtype of type 'List'. Therefore, we need to make a type discrimination of the reader.result. --- lib/_internal/file_picker_web.dart | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/_internal/file_picker_web.dart b/lib/_internal/file_picker_web.dart index cb432cf2..3bc4c31f 100644 --- a/lib/_internal/file_picker_web.dart +++ b/lib/_internal/file_picker_web.dart @@ -208,6 +208,15 @@ class FilePickerWeb extends FilePicker { final blob = file.slice(start, end); reader.readAsArrayBuffer(blob); await EventStreamProviders.loadEvent.forTarget(reader).first; + if (reader.result is ByteBuffer){ + // When built in the html web-renderer, an error may occur: + // TypeError: Instance of 'NativeByteBuffer': type 'NativeByteBuffer' is not a subtype of type 'List'. + // Therefore, we need to make a type discrimination of the reader.result. + yield (reader.result as ByteBuffer).asUint8List(); + start += _readStreamChunkSize; + continue; + } + yield reader.result as List; start += _readStreamChunkSize; } From 958db225ef755388f09420a7974dccee67fc0a49 Mon Sep 17 00:00:00 2001 From: vito-go <56462631+vito-go@users.noreply.github.com> Date: Wed, 24 Apr 2024 17:14:20 +0800 Subject: [PATCH 2/8] Update file_picker_web.dart Apply suggestions from code review Co-authored-by: Navaron Bracke readerResult --- lib/_internal/file_picker_web.dart | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/_internal/file_picker_web.dart b/lib/_internal/file_picker_web.dart index 3bc4c31f..0c842297 100644 --- a/lib/_internal/file_picker_web.dart +++ b/lib/_internal/file_picker_web.dart @@ -208,16 +208,33 @@ class FilePickerWeb extends FilePicker { final blob = file.slice(start, end); reader.readAsArrayBuffer(blob); await EventStreamProviders.loadEvent.forTarget(reader).first; - if (reader.result is ByteBuffer){ - // When built in the html web-renderer, an error may occur: + final JSAny? readerResult = reader.result; + if (readerResult == null) { + continue; + } + if (readerResult is ByteBuffer) { + // An error may occur: // TypeError: Instance of 'NativeByteBuffer': type 'NativeByteBuffer' is not a subtype of type 'List'. // Therefore, we need to make a type discrimination of the reader.result. - yield (reader.result as ByteBuffer).asUint8List(); + yield (reader.result as ByteBuffer).asUint8List(); + start += _readStreamChunkSize; + continue; + } + // Handle byte buffers. + if (readerResult.instanceOfString('JSArrayBuffer')) { + yield (readerResult as JSArrayBuffer).toDart.asUint8List(); + start += _readStreamChunkSize; + continue; + } + // Handle JS Array type. + if (readerResult.instanceOfString('Array')) { + // Assume this is a List. + yield (readerResult as JSArray).toDart.cast(); start += _readStreamChunkSize; continue; } - - yield reader.result as List; + // Default this is a List + yield readerResult as List; start += _readStreamChunkSize; } } From aacdb446751b71ac9e7e7bd6d241f5249761edbe Mon Sep 17 00:00:00 2001 From: liushihao Date: Thu, 25 Apr 2024 19:43:23 +0800 Subject: [PATCH 3/8] readerResult --- lib/_internal/file_picker_web.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/_internal/file_picker_web.dart b/lib/_internal/file_picker_web.dart index 0c842297..da771b7b 100644 --- a/lib/_internal/file_picker_web.dart +++ b/lib/_internal/file_picker_web.dart @@ -216,7 +216,7 @@ class FilePickerWeb extends FilePicker { // An error may occur: // TypeError: Instance of 'NativeByteBuffer': type 'NativeByteBuffer' is not a subtype of type 'List'. // Therefore, we need to make a type discrimination of the reader.result. - yield (reader.result as ByteBuffer).asUint8List(); + yield (readerResult as ByteBuffer).asUint8List(); start += _readStreamChunkSize; continue; } From b70d867c50c9c0de041dbd184bda19d8fa9a6afa Mon Sep 17 00:00:00 2001 From: liushihao Date: Thu, 25 Apr 2024 22:58:21 +0800 Subject: [PATCH 4/8] Handle JS ArrayBuffer type. --- lib/_internal/file_picker_web.dart | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/_internal/file_picker_web.dart b/lib/_internal/file_picker_web.dart index da771b7b..fe283d8b 100644 --- a/lib/_internal/file_picker_web.dart +++ b/lib/_internal/file_picker_web.dart @@ -220,22 +220,23 @@ class FilePickerWeb extends FilePicker { start += _readStreamChunkSize; continue; } + // Defensive programming: maybe the code can not reach here. ByteBuffer can cover most cases. // Handle byte buffers. if (readerResult.instanceOfString('JSArrayBuffer')) { yield (readerResult as JSArrayBuffer).toDart.asUint8List(); start += _readStreamChunkSize; - continue; } // Handle JS Array type. if (readerResult.instanceOfString('Array')) { // Assume this is a List. yield (readerResult as JSArray).toDart.cast(); start += _readStreamChunkSize; - continue; } - // Default this is a List - yield readerResult as List; - start += _readStreamChunkSize; + // Handle JS ArrayBuffer type. + if (readerResult.instanceOfString('ArrayBuffer')) { + yield (readerResult as JSArrayBuffer).toDart.asUint8List(); + start += _readStreamChunkSize; + } } } } From 4dbec8122c8e8ff7362ef8e03ac332d3481c07a1 Mon Sep 17 00:00:00 2001 From: liushihao Date: Fri, 26 Apr 2024 22:25:52 +0800 Subject: [PATCH 5/8] Apply suggestions from code review Co-authored-by: Navaron Bracke --- lib/_internal/file_picker_web.dart | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/lib/_internal/file_picker_web.dart b/lib/_internal/file_picker_web.dart index fe283d8b..f43f127a 100644 --- a/lib/_internal/file_picker_web.dart +++ b/lib/_internal/file_picker_web.dart @@ -212,31 +212,20 @@ class FilePickerWeb extends FilePicker { if (readerResult == null) { continue; } - if (readerResult is ByteBuffer) { - // An error may occur: - // TypeError: Instance of 'NativeByteBuffer': type 'NativeByteBuffer' is not a subtype of type 'List'. - // Therefore, we need to make a type discrimination of the reader.result. - yield (readerResult as ByteBuffer).asUint8List(); - start += _readStreamChunkSize; - continue; - } - // Defensive programming: maybe the code can not reach here. ByteBuffer can cover most cases. - // Handle byte buffers. - if (readerResult.instanceOfString('JSArrayBuffer')) { + // TODO: use `isA()` when switching to Dart 3.4 + // Handle the ArrayBuffer type. This maps to a `ByteBuffer` in Dart. + if (readerResult.instanceOfString('ArrayBuffer')) { yield (readerResult as JSArrayBuffer).toDart.asUint8List(); start += _readStreamChunkSize; + continue; } - // Handle JS Array type. + // TODO: use `isA()` when switching to Dart 3.4 + // Handle the Array type. if (readerResult.instanceOfString('Array')) { // Assume this is a List. yield (readerResult as JSArray).toDart.cast(); start += _readStreamChunkSize; } - // Handle JS ArrayBuffer type. - if (readerResult.instanceOfString('ArrayBuffer')) { - yield (readerResult as JSArrayBuffer).toDart.asUint8List(); - start += _readStreamChunkSize; - } } } } From 7c44eb1e27f427b389c12a06919f1f4a728e5b4b Mon Sep 17 00:00:00 2001 From: liushihao Date: Fri, 26 Apr 2024 22:57:51 +0800 Subject: [PATCH 6/8] Update CHANGELOG and version to 8.0.3 --- CHANGELOG.md | 4 ++++ pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bbce0a9..7c34121e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 8.0.3 +### Web +Fixes a issue of "TypeError" about `_openFileReadStream`[#1322](https://github.com/miguelpruivo/flutter_file_picker/pull/1496). + ## 8.0.2 ### iOS Fixes the bug [#1412](https://github.com/miguelpruivo/flutter_file_picker/issues/1412) that picking a folder in iOS causes the original folder to be deleted. diff --git a/pubspec.yaml b/pubspec.yaml index 2a344f82..8f2c8b16 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ description: A package that allows you to use a native file explorer to pick sin homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker repository: https://github.com/miguelpruivo/flutter_file_picker issue_tracker: https://github.com/miguelpruivo/flutter_file_picker/issues -version: 8.0.2 +version: 8.0.3 dependencies: flutter: From 130f02a46086110a094a0db8dc2ca51e00d177d2 Mon Sep 17 00:00:00 2001 From: vito-go <56462631+vito-go@users.noreply.github.com> Date: Fri, 26 Apr 2024 23:01:04 +0800 Subject: [PATCH 7/8] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c34121e..d7dc252f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## 8.0.3 ### Web -Fixes a issue of "TypeError" about `_openFileReadStream`[#1322](https://github.com/miguelpruivo/flutter_file_picker/pull/1496). +Fixes a issue of "TypeError" about `_openFileReadStream`[#1496](https://github.com/miguelpruivo/flutter_file_picker/pull/1496). ## 8.0.2 ### iOS From 2357b8d2ee9ee77fa1726bc1ec16e823ce3ced4c Mon Sep 17 00:00:00 2001 From: Navaron Bracke Date: Sat, 27 Apr 2024 09:59:34 +0200 Subject: [PATCH 8/8] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7dc252f..0ce1feee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## 8.0.3 ### Web -Fixes a issue of "TypeError" about `_openFileReadStream`[#1496](https://github.com/miguelpruivo/flutter_file_picker/pull/1496). +Fixes a TypeError with `pickFiles()` when using the HTML renderer. ## 8.0.2 ### iOS