Skip to content

Commit

Permalink
[file_selector] Migrate to null safety (flutter#3631)
Browse files Browse the repository at this point in the history
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#75235
  • Loading branch information
stuartmorgan authored Feb 25, 2021
1 parent fff1420 commit 1de6d96
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 138 deletions.
4 changes: 4 additions & 0 deletions packages/file_selector/file_selector/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.0

Migrate to null safety.

## 0.7.0+2

* Update the example app: remove the deprecated `RaisedButton` and `FlatButton` widgets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import 'package:flutter/material.dart';
class GetDirectoryPage extends StatelessWidget {
void _getDirectoryPath(BuildContext context) async {
final String confirmButtonText = 'Choose';
final String directoryPath = await getDirectoryPath(
final String? directoryPath = await getDirectoryPath(
confirmButtonText: confirmButtonText,
);
if (directoryPath == null) {
// Operation was canceled by the user.
return;
}
await showDialog(
context: context,
builder: (context) => TextDisplay(directoryPath),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class OpenImagePage extends StatelessWidget {
extensions: ['jpg', 'png'],
);
final List<XFile> files = await openFiles(acceptedTypeGroups: [typeGroup]);
if (files.isEmpty) {
// Operation was canceled by the user.
return;
}
final XFile file = files[0];
final String fileName = file.name;
final String filePath = file.path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class OpenMultipleImagesPage extends StatelessWidget {
jpgsTypeGroup,
pngTypeGroup,
]);
if (files.isEmpty) {
// Operation was canceled by the user.
return;
}
await showDialog(
context: context,
builder: (context) => MultipleImagesDisplay(files),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ class OpenTextPage extends StatelessWidget {
label: 'text',
extensions: ['txt', 'json'],
);
final XFile file = await openFile(acceptedTypeGroups: [typeGroup]);
final XFile? file = await openFile(acceptedTypeGroups: [typeGroup]);
if (file == null) {
// Operation was canceled by the user.
return;
}
final String fileName = file.name;
final String fileContent = await file.readAsString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ class SaveTextPage extends StatelessWidget {
final TextEditingController _contentController = TextEditingController();

void _saveFile() async {
final String path = await getSavePath();
String? path = await getSavePath();
if (path == null) {
// Operation was canceled by the user.
return;
}
final String text = _contentController.text;
final String fileName = _nameController.text;
final Uint8List fileData = Uint8List.fromList(text.codeUnits);
Expand Down
57 changes: 1 addition & 56 deletions packages/file_selector/file_selector/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
name: example
description: A new Flutter project.

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

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

environment:
sdk: ">=2.7.0 <3.0.0"
sdk: ">=2.12.0-259.9.beta <3.0.0"

dependencies:
flutter:
Expand All @@ -32,52 +20,9 @@ dependencies:
# the parent directory to use the current plugin's version.
path: ../

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.3

dev_dependencies:
flutter_test:
sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true

# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.

# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages

# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
30 changes: 15 additions & 15 deletions packages/file_selector/file_selector/lib/file_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export 'package:file_selector_platform_interface/file_selector_platform_interfac
show XFile, XTypeGroup;

/// Open file dialog for loading files and return a file path
Future<XFile> openFile({
List<XTypeGroup> acceptedTypeGroups,
String initialDirectory,
String confirmButtonText,
Future<XFile?> openFile({
List<XTypeGroup> acceptedTypeGroups = const <XTypeGroup>[],
String? initialDirectory,
String? confirmButtonText,
}) {
return FileSelectorPlatform.instance.openFile(
acceptedTypeGroups: acceptedTypeGroups,
Expand All @@ -23,9 +23,9 @@ Future<XFile> openFile({

/// Open file dialog for loading files and return a list of file paths
Future<List<XFile>> openFiles({
List<XTypeGroup> acceptedTypeGroups,
String initialDirectory,
String confirmButtonText,
List<XTypeGroup> acceptedTypeGroups = const <XTypeGroup>[],
String? initialDirectory,
String? confirmButtonText,
}) {
return FileSelectorPlatform.instance.openFiles(
acceptedTypeGroups: acceptedTypeGroups,
Expand All @@ -34,11 +34,11 @@ Future<List<XFile>> openFiles({
}

/// Saves File to user's file system
Future<String> getSavePath({
List<XTypeGroup> acceptedTypeGroups,
String initialDirectory,
String suggestedName,
String confirmButtonText,
Future<String?> getSavePath({
List<XTypeGroup> acceptedTypeGroups = const <XTypeGroup>[],
String? initialDirectory,
String? suggestedName,
String? confirmButtonText,
}) async {
return FileSelectorPlatform.instance.getSavePath(
acceptedTypeGroups: acceptedTypeGroups,
Expand All @@ -48,9 +48,9 @@ Future<String> getSavePath({
}

/// Gets a directory path from a user's file system
Future<String> getDirectoryPath({
String initialDirectory,
String confirmButtonText,
Future<String?> getDirectoryPath({
String? initialDirectory,
String? confirmButtonText,
}) async {
return FileSelectorPlatform.instance.getDirectoryPath(
initialDirectory: initialDirectory, confirmButtonText: confirmButtonText);
Expand Down
15 changes: 7 additions & 8 deletions packages/file_selector/file_selector/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
name: file_selector
description: Flutter plugin for opening and saving files.
homepage: https://github.com/flutter/plugins/tree/master/packages/file_selector/file_selector
version: 0.7.0+2
version: 0.8.0

dependencies:
flutter:
sdk: flutter
file_selector_platform_interface: ^1.0.0
file_selector_platform_interface: ^2.0.0

dev_dependencies:
flutter_test:
sdk: flutter
test: ^1.3.0
mockito: ^4.1.1
plugin_platform_interface: ^1.0.0
pedantic: ^1.8.0
test: ^1.16.3
plugin_platform_interface: ">=1.0.0 <3.0.0"
pedantic: ^1.10.0

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

0 comments on commit 1de6d96

Please sign in to comment.