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

[file_selector] Migrate to null safety #3631

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional nits: I'd prefer empty list and empty strings as default none values.
But since the platform interface has already landed, it's probably not worth to go back and introduce a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make this an empty set without changing the platform interface (and that makes it much easier to change the platform interface later); I'll do that now. I should have caught it during initial review but wasn't as familiar with NNBD migrations yet.

For the strings, I don't think empty strings are valid for this API. An empty initial directory is somewhat ambiguous; is that intended to be the device root, for instance? And there's no fundamental reason someone couldn't decide to make the confirm button blank with an empty string (it would be bad UI design, but so are many other valid strings).

Empty collections are I think almost alway going to be the right choice, but empty strings need real thought about whether there's really no difference between empty and null. Here, that's definitely not the case for most of them.

Copy link
Contributor Author

@stuartmorgan stuartmorgan Feb 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I checked the web implementation, and it already appears to treat empty sets and null the same. This will break one of the unendorsed desktop implementations, but I can fix that trivially.)

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,
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,
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