Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 1de6d96

Browse files
[file_selector] Migrate to null safety (#3631)
Migrates the app-facing package to null safety. Includes replacing Mockito with a custom fake/mock. Fixes an issue where the example didn't handle dialogs being canceled, which was highlighted by the NNBD migration. (Previously, they would cause null assertions at runtime, which wasn't noticed during development. NNBD for the win!) Fixes flutter/flutter#75235
1 parent fff1420 commit 1de6d96

10 files changed

+197
-138
lines changed

packages/file_selector/file_selector/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.8.0
2+
3+
Migrate to null safety.
4+
15
## 0.7.0+2
26

37
* Update the example app: remove the deprecated `RaisedButton` and `FlatButton` widgets.

packages/file_selector/file_selector/example/lib/get_directory_page.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import 'package:flutter/material.dart';
55
class GetDirectoryPage extends StatelessWidget {
66
void _getDirectoryPath(BuildContext context) async {
77
final String confirmButtonText = 'Choose';
8-
final String directoryPath = await getDirectoryPath(
8+
final String? directoryPath = await getDirectoryPath(
99
confirmButtonText: confirmButtonText,
1010
);
11+
if (directoryPath == null) {
12+
// Operation was canceled by the user.
13+
return;
14+
}
1115
await showDialog(
1216
context: context,
1317
builder: (context) => TextDisplay(directoryPath),

packages/file_selector/file_selector/example/lib/open_image_page.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class OpenImagePage extends StatelessWidget {
1111
extensions: ['jpg', 'png'],
1212
);
1313
final List<XFile> files = await openFiles(acceptedTypeGroups: [typeGroup]);
14+
if (files.isEmpty) {
15+
// Operation was canceled by the user.
16+
return;
17+
}
1418
final XFile file = files[0];
1519
final String fileName = file.name;
1620
final String filePath = file.path;

packages/file_selector/file_selector/example/lib/open_multiple_images_page.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class OpenMultipleImagesPage extends StatelessWidget {
1818
jpgsTypeGroup,
1919
pngTypeGroup,
2020
]);
21+
if (files.isEmpty) {
22+
// Operation was canceled by the user.
23+
return;
24+
}
2125
await showDialog(
2226
context: context,
2327
builder: (context) => MultipleImagesDisplay(files),

packages/file_selector/file_selector/example/lib/open_text_page.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ class OpenTextPage extends StatelessWidget {
88
label: 'text',
99
extensions: ['txt', 'json'],
1010
);
11-
final XFile file = await openFile(acceptedTypeGroups: [typeGroup]);
11+
final XFile? file = await openFile(acceptedTypeGroups: [typeGroup]);
12+
if (file == null) {
13+
// Operation was canceled by the user.
14+
return;
15+
}
1216
final String fileName = file.name;
1317
final String fileContent = await file.readAsString();
1418

packages/file_selector/file_selector/example/lib/save_text_page.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ class SaveTextPage extends StatelessWidget {
88
final TextEditingController _contentController = TextEditingController();
99

1010
void _saveFile() async {
11-
final String path = await getSavePath();
11+
String? path = await getSavePath();
12+
if (path == null) {
13+
// Operation was canceled by the user.
14+
return;
15+
}
1216
final String text = _contentController.text;
1317
final String fileName = _nameController.text;
1418
final Uint8List fileData = Uint8List.fromList(text.codeUnits);
Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
11
name: example
22
description: A new Flutter project.
33

4-
# The following line prevents the package from being accidentally published to
5-
# pub.dev using `pub publish`. This is preferred for private packages.
64
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
75

8-
# The following defines the version and build number for your application.
9-
# A version number is three numbers separated by dots, like 1.2.43
10-
# followed by an optional build number separated by a +.
11-
# Both the version and the builder number may be overridden in flutter
12-
# build by specifying --build-name and --build-number, respectively.
13-
# In Android, build-name is used as versionName while build-number used as versionCode.
14-
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
15-
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
16-
# Read more about iOS versioning at
17-
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
186
version: 1.0.0+1
197

208
environment:
21-
sdk: ">=2.7.0 <3.0.0"
9+
sdk: ">=2.12.0-259.9.beta <3.0.0"
2210

2311
dependencies:
2412
flutter:
@@ -32,52 +20,9 @@ dependencies:
3220
# the parent directory to use the current plugin's version.
3321
path: ../
3422

35-
# The following adds the Cupertino Icons font to your application.
36-
# Use with the CupertinoIcons class for iOS style icons.
37-
cupertino_icons: ^0.1.3
38-
3923
dev_dependencies:
4024
flutter_test:
4125
sdk: flutter
4226

43-
# For information on the generic Dart part of this file, see the
44-
# following page: https://dart.dev/tools/pub/pubspec
45-
46-
# The following section is specific to Flutter.
4727
flutter:
48-
49-
# The following line ensures that the Material Icons font is
50-
# included with your application, so that you can use the icons in
51-
# the material Icons class.
5228
uses-material-design: true
53-
54-
# To add assets to your application, add an assets section, like this:
55-
# assets:
56-
# - images/a_dot_burr.jpeg
57-
# - images/a_dot_ham.jpeg
58-
59-
# An image asset can refer to one or more resolution-specific "variants", see
60-
# https://flutter.dev/assets-and-images/#resolution-aware.
61-
62-
# For details regarding adding assets from package dependencies, see
63-
# https://flutter.dev/assets-and-images/#from-packages
64-
65-
# To add custom fonts to your application, add a fonts section here,
66-
# in this "flutter" section. Each entry in this list should have a
67-
# "family" key with the font family name, and a "fonts" key with a
68-
# list giving the asset and other descriptors for the font. For
69-
# example:
70-
# fonts:
71-
# - family: Schyler
72-
# fonts:
73-
# - asset: fonts/Schyler-Regular.ttf
74-
# - asset: fonts/Schyler-Italic.ttf
75-
# style: italic
76-
# - family: Trajan Pro
77-
# fonts:
78-
# - asset: fonts/TrajanPro.ttf
79-
# - asset: fonts/TrajanPro_Bold.ttf
80-
# weight: 700
81-
#
82-
# For details regarding fonts from package dependencies,
83-
# see https://flutter.dev/custom-fonts/#from-packages

packages/file_selector/file_selector/lib/file_selector.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ export 'package:file_selector_platform_interface/file_selector_platform_interfac
1010
show XFile, XTypeGroup;
1111

1212
/// Open file dialog for loading files and return a file path
13-
Future<XFile> openFile({
14-
List<XTypeGroup> acceptedTypeGroups,
15-
String initialDirectory,
16-
String confirmButtonText,
13+
Future<XFile?> openFile({
14+
List<XTypeGroup> acceptedTypeGroups = const <XTypeGroup>[],
15+
String? initialDirectory,
16+
String? confirmButtonText,
1717
}) {
1818
return FileSelectorPlatform.instance.openFile(
1919
acceptedTypeGroups: acceptedTypeGroups,
@@ -23,9 +23,9 @@ Future<XFile> openFile({
2323

2424
/// Open file dialog for loading files and return a list of file paths
2525
Future<List<XFile>> openFiles({
26-
List<XTypeGroup> acceptedTypeGroups,
27-
String initialDirectory,
28-
String confirmButtonText,
26+
List<XTypeGroup> acceptedTypeGroups = const <XTypeGroup>[],
27+
String? initialDirectory,
28+
String? confirmButtonText,
2929
}) {
3030
return FileSelectorPlatform.instance.openFiles(
3131
acceptedTypeGroups: acceptedTypeGroups,
@@ -34,11 +34,11 @@ Future<List<XFile>> openFiles({
3434
}
3535

3636
/// Saves File to user's file system
37-
Future<String> getSavePath({
38-
List<XTypeGroup> acceptedTypeGroups,
39-
String initialDirectory,
40-
String suggestedName,
41-
String confirmButtonText,
37+
Future<String?> getSavePath({
38+
List<XTypeGroup> acceptedTypeGroups = const <XTypeGroup>[],
39+
String? initialDirectory,
40+
String? suggestedName,
41+
String? confirmButtonText,
4242
}) async {
4343
return FileSelectorPlatform.instance.getSavePath(
4444
acceptedTypeGroups: acceptedTypeGroups,
@@ -48,9 +48,9 @@ Future<String> getSavePath({
4848
}
4949

5050
/// Gets a directory path from a user's file system
51-
Future<String> getDirectoryPath({
52-
String initialDirectory,
53-
String confirmButtonText,
51+
Future<String?> getDirectoryPath({
52+
String? initialDirectory,
53+
String? confirmButtonText,
5454
}) async {
5555
return FileSelectorPlatform.instance.getDirectoryPath(
5656
initialDirectory: initialDirectory, confirmButtonText: confirmButtonText);
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
name: file_selector
22
description: Flutter plugin for opening and saving files.
33
homepage: https://github.com/flutter/plugins/tree/master/packages/file_selector/file_selector
4-
version: 0.7.0+2
4+
version: 0.8.0
55

66
dependencies:
77
flutter:
88
sdk: flutter
9-
file_selector_platform_interface: ^1.0.0
9+
file_selector_platform_interface: ^2.0.0
1010

1111
dev_dependencies:
1212
flutter_test:
1313
sdk: flutter
14-
test: ^1.3.0
15-
mockito: ^4.1.1
16-
plugin_platform_interface: ^1.0.0
17-
pedantic: ^1.8.0
14+
test: ^1.16.3
15+
plugin_platform_interface: ">=1.0.0 <3.0.0"
16+
pedantic: ^1.10.0
1817

1918
environment:
20-
sdk: ">=2.1.0 <3.0.0"
21-
flutter: ">=1.12.13+hotfix.5"
19+
sdk: ">=2.12.0-259.9.beta <3.0.0"
20+
flutter: ">=1.20.0"

0 commit comments

Comments
 (0)