Skip to content

Commit

Permalink
[path_provider] Migrate examples to null-safety (flutter#3559)
Browse files Browse the repository at this point in the history
Allows running the examples in strong mode, even though the integration tests can't yet be.

Converts the macOS example to use the platform interface, rather than the app-facing package, to eliminate the circular dependency. Also does some cleanup and simplification of the desktop example pubspecs.

Does not update versions/changelogs since this won't be explicitly published, given that it's example-only.
  • Loading branch information
stuartmorgan authored and adsonpleal committed Feb 26, 2021
1 parent f3f1328 commit c7514d2
Show file tree
Hide file tree
Showing 21 changed files with 162 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

import 'dart:async';

import 'dart:io';
Expand Down
38 changes: 19 additions & 19 deletions packages/path_provider/path_provider/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ class MyApp extends StatelessWidget {
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
Future<Directory> _tempDirectory;
Future<Directory> _appSupportDirectory;
Future<Directory> _appLibraryDirectory;
Future<Directory> _appDocumentsDirectory;
Future<Directory> _externalDocumentsDirectory;
Future<List<Directory>> _externalStorageDirectories;
Future<List<Directory>> _externalCacheDirectories;
Future<Directory?>? _tempDirectory;
Future<Directory?>? _appSupportDirectory;
Future<Directory?>? _appLibraryDirectory;
Future<Directory?>? _appDocumentsDirectory;
Future<Directory?>? _externalDocumentsDirectory;
Future<List<Directory>?>? _externalStorageDirectories;
Future<List<Directory>?>? _externalCacheDirectories;

void _requestTempDirectory() {
setState(() {
Expand All @@ -51,13 +51,13 @@ class _MyHomePageState extends State<MyHomePage> {
}

Widget _buildDirectory(
BuildContext context, AsyncSnapshot<Directory> snapshot) {
BuildContext context, AsyncSnapshot<Directory?> snapshot) {
Text text = const Text('');
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
text = Text('Error: ${snapshot.error}');
} else if (snapshot.hasData) {
text = Text('path: ${snapshot.data.path}');
text = Text('path: ${snapshot.data!.path}');
} else {
text = const Text('path unavailable');
}
Expand All @@ -66,14 +66,14 @@ class _MyHomePageState extends State<MyHomePage> {
}

Widget _buildDirectories(
BuildContext context, AsyncSnapshot<List<Directory>> snapshot) {
BuildContext context, AsyncSnapshot<List<Directory>?> snapshot) {
Text text = const Text('');
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
text = Text('Error: ${snapshot.error}');
} else if (snapshot.hasData) {
final String combined =
snapshot.data.map((Directory d) => d.path).join(', ');
snapshot.data!.map((Directory d) => d.path).join(', ');
text = Text('paths: $combined');
} else {
text = const Text('path unavailable');
Expand Down Expand Up @@ -134,7 +134,7 @@ class _MyHomePageState extends State<MyHomePage> {
onPressed: _requestTempDirectory,
),
),
FutureBuilder<Directory>(
FutureBuilder<Directory?>(
future: _tempDirectory, builder: _buildDirectory),
Padding(
padding: const EdgeInsets.all(16.0),
Expand All @@ -143,7 +143,7 @@ class _MyHomePageState extends State<MyHomePage> {
onPressed: _requestAppDocumentsDirectory,
),
),
FutureBuilder<Directory>(
FutureBuilder<Directory?>(
future: _appDocumentsDirectory, builder: _buildDirectory),
Padding(
padding: const EdgeInsets.all(16.0),
Expand All @@ -152,7 +152,7 @@ class _MyHomePageState extends State<MyHomePage> {
onPressed: _requestAppSupportDirectory,
),
),
FutureBuilder<Directory>(
FutureBuilder<Directory?>(
future: _appSupportDirectory, builder: _buildDirectory),
Padding(
padding: const EdgeInsets.all(16.0),
Expand All @@ -161,7 +161,7 @@ class _MyHomePageState extends State<MyHomePage> {
onPressed: _requestAppLibraryDirectory,
),
),
FutureBuilder<Directory>(
FutureBuilder<Directory?>(
future: _appLibraryDirectory, builder: _buildDirectory),
Padding(
padding: const EdgeInsets.all(16.0),
Expand All @@ -172,7 +172,7 @@ class _MyHomePageState extends State<MyHomePage> {
Platform.isIOS ? null : _requestExternalStorageDirectory,
),
),
FutureBuilder<Directory>(
FutureBuilder<Directory?>(
future: _externalDocumentsDirectory, builder: _buildDirectory),
Column(children: <Widget>[
Padding(
Expand All @@ -190,7 +190,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
),
]),
FutureBuilder<List<Directory>>(
FutureBuilder<List<Directory>?>(
future: _externalStorageDirectories,
builder: _buildDirectories),
Column(children: <Widget>[
Expand All @@ -204,7 +204,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
),
]),
FutureBuilder<List<Directory>>(
FutureBuilder<List<Directory>?>(
future: _externalCacheDirectories, builder: _buildDirectories),
],
),
Expand Down
2 changes: 1 addition & 1 deletion packages/path_provider/path_provider/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ flutter:
uses-material-design: true

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.12.0-0 <3.0.0"
flutter: ">=1.12.13+hotfix.5"
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

import 'dart:async';
import 'dart:convert';
import 'dart:io';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:path_provider_linux/path_provider_linux.dart';
Expand Down
18 changes: 9 additions & 9 deletions packages/path_provider/path_provider_linux/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:async';
import 'package:flutter/services.dart';
import 'package:path_provider_linux/path_provider_linux.dart';

void main() async {
void main() {
runApp(MyApp());
}

Expand All @@ -15,10 +15,10 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
String _tempDirectory = 'Unknown';
String _downloadsDirectory = 'Unknown';
String _appSupportDirectory = 'Unknown';
String _documentsDirectory = 'Unknown';
String? _tempDirectory = 'Unknown';
String? _downloadsDirectory = 'Unknown';
String? _appSupportDirectory = 'Unknown';
String? _documentsDirectory = 'Unknown';
final PathProviderLinux _provider = PathProviderLinux();

@override
Expand All @@ -29,10 +29,10 @@ class _MyAppState extends State<MyApp> {

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initDirectories() async {
String tempDirectory;
String downloadsDirectory;
String appSupportDirectory;
String documentsDirectory;
String? tempDirectory;
String? downloadsDirectory;
String? appSupportDirectory;
String? documentsDirectory;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
tempDirectory = await _provider.getTemporaryPath();
Expand Down
44 changes: 2 additions & 42 deletions packages/path_provider/path_provider_linux/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ description: Demonstrates how to use the path_provider_linux plugin.
publish_to: "none"

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.12.0-0 <3.0.0"
flutter: ">=1.10.0"

dependencies:
flutter:
sdk: flutter

path_provider_linux: any

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

dependency_overrides:
path_provider_linux:
# When depending on this package from a real application you should use:
# path_provider_linux: ^x.y.z
Expand All @@ -32,39 +26,5 @@ dev_dependencies:
integration_test:
path: ../../../integration_test

# 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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void main() {
find.byWidgetPredicate(
(Widget widget) =>
widget is Text &&
widget.data.startsWith('Temp Directory: /tmp'),
widget.data!.startsWith('Temp Directory: /tmp'),
),
findsOneWidget,
);
Expand All @@ -48,7 +48,7 @@ void main() {
find.byWidgetPredicate(
(Widget widget) =>
widget is Text &&
widget.data.startsWith('Documents Directory: /'),
widget.data!.startsWith('Documents Directory: /'),
),
findsOneWidget,
);
Expand All @@ -66,7 +66,7 @@ void main() {
find.byWidgetPredicate(
(Widget widget) =>
widget is Text &&
widget.data.startsWith('Downloads Directory: /'),
widget.data!.startsWith('Downloads Directory: /'),
),
findsOneWidget,
);
Expand All @@ -85,7 +85,7 @@ void main() {
find.byWidgetPredicate(
(Widget widget) =>
widget is Text &&
widget.data.startsWith('Application Support Directory: /'),
widget.data!.startsWith('Application Support Directory: /'),
),
findsOneWidget,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

import 'dart:async';
import 'dart:convert';
import 'dart:io';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,56 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
import 'package:integration_test/integration_test.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

testWidgets('getTemporaryDirectory', (WidgetTester tester) async {
final Directory result = await getTemporaryDirectory();
final PathProviderPlatform provider = PathProviderPlatform.instance;
final String result = await provider.getTemporaryPath();
_verifySampleFile(result, 'temporaryDirectory');
});

testWidgets('getApplicationDocumentsDirectory', (WidgetTester tester) async {
final Directory result = await getApplicationDocumentsDirectory();
final PathProviderPlatform provider = PathProviderPlatform.instance;
final String result = await provider.getApplicationDocumentsPath();
_verifySampleFile(result, 'applicationDocuments');
});

testWidgets('getApplicationSupportDirectory', (WidgetTester tester) async {
final Directory result = await getApplicationSupportDirectory();
final PathProviderPlatform provider = PathProviderPlatform.instance;
final String result = await provider.getApplicationSupportPath();
_verifySampleFile(result, 'applicationSupport');
});

testWidgets('getLibraryDirectory', (WidgetTester tester) async {
if (!Platform.isMacOS) {
return;
}
final Directory result = await getLibraryDirectory();
final PathProviderPlatform provider = PathProviderPlatform.instance;
final String result = await provider.getLibraryPath();
_verifySampleFile(result, 'library');
});

testWidgets('getDownloadsDirectory', (WidgetTester tester) async {
final PathProviderPlatform provider = PathProviderPlatform.instance;
final String result = await provider.getDownloadsPath();
// _verifySampleFile causes hangs in driver for some reason, so just
// validate that a non-empty path was returned.
expect(result, isNotEmpty);
});
}

/// Verify a file called [name] in [directory] by recreating it with test
/// Verify a file called [name] in [directoryPath] by recreating it with test
/// contents when necessary.
void _verifySampleFile(Directory directory, String name) {
final File file = File('${directory.path}/$name');
///
/// If [createDirectory] is true, the directory will be created if missing.
void _verifySampleFile(String directoryPath, String name) {
final Directory directory = Directory(directoryPath);
final File file = File('${directory.path}${Platform.pathSeparator}$name');

if (file.existsSync()) {
file.deleteSync();
Expand Down
Loading

0 comments on commit c7514d2

Please sign in to comment.