Skip to content

Commit

Permalink
[path_provider_linux] Migrate to null safety (flutter#3330)
Browse files Browse the repository at this point in the history
  • Loading branch information
yash1200 authored and adsonpleal committed Feb 26, 2021
1 parent 6c47eeb commit 714913e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 26 deletions.
4 changes: 4 additions & 0 deletions packages/path_provider/path_provider_linux/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.0-nullsafety

* Migrate to null safety.

## 0.1.1+3

* Update Flutter SDK constraint.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,45 @@

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

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

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

testWidgets('getDownloadDirectory', (WidgetTester tester) async {
if (!Platform.isLinux) {
return;
}
final Directory result = await getDownloadsDirectory();
final PathProviderLinux provider = PathProviderLinux();
final String result = await provider.getDownloadsPath();
_verifySampleFile(result, 'downloadDirectory');
});

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

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

/// 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');
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
11 changes: 6 additions & 5 deletions packages/path_provider/path_provider_linux/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path_provider_linux/path_provider_linux.dart';

void main() async {
runApp(MyApp());
Expand All @@ -19,6 +19,7 @@ class _MyAppState extends State<MyApp> {
String _downloadsDirectory = 'Unknown';
String _appSupportDirectory = 'Unknown';
String _documentsDirectory = 'Unknown';
final PathProviderLinux _provider = PathProviderLinux();

@override
void initState() {
Expand All @@ -34,27 +35,27 @@ class _MyAppState extends State<MyApp> {
String documentsDirectory;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
tempDirectory = (await getTemporaryDirectory()).path;
tempDirectory = await _provider.getTemporaryPath();
} on PlatformException catch (e, stackTrace) {
tempDirectory = 'Failed to get temp directory.';
print('$tempDirectory $e $stackTrace');
}
try {
downloadsDirectory = (await getDownloadsDirectory()).path;
downloadsDirectory = await _provider.getDownloadsPath();
} on PlatformException catch (e, stackTrace) {
downloadsDirectory = 'Failed to get downloads directory.';
print('$downloadsDirectory $e $stackTrace');
}

try {
documentsDirectory = (await getApplicationDocumentsDirectory()).path;
documentsDirectory = await _provider.getApplicationDocumentsPath();
} on PlatformException catch (e, stackTrace) {
documentsDirectory = 'Failed to get documents directory.';
print('$documentsDirectory $e $stackTrace');
}

try {
appSupportDirectory = (await getApplicationSupportDirectory()).path;
appSupportDirectory = await _provider.getApplicationSupportPath();
} on PlatformException catch (e, stackTrace) {
appSupportDirectory = 'Failed to get documents directory.';
print('$appSupportDirectory $e $stackTrace');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies:
flutter:
sdk: flutter

path_provider: ^1.6.10
path_provider_linux: any

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class PathProviderLinux extends PathProviderPlatform {
}

@override
Future<String> getTemporaryPath() {
Future<String?> getTemporaryPath() {
return Future.value("/tmp");
}

@override
Future<String> getApplicationSupportPath() async {
Future<String?> getApplicationSupportPath() async {
final processName = path.basenameWithoutExtension(
await File('/proc/self/exe').resolveSymbolicLinks());
final directory = Directory(path.join(xdg.dataHome.path, processName));
Expand All @@ -35,12 +35,12 @@ class PathProviderLinux extends PathProviderPlatform {
}

@override
Future<String> getApplicationDocumentsPath() {
return Future.value(xdg.getUserDirectory('DOCUMENTS').path);
Future<String?> getApplicationDocumentsPath() {
return Future.value(xdg.getUserDirectory('DOCUMENTS')?.path);
}

@override
Future<String> getDownloadsPath() {
return Future.value(xdg.getUserDirectory('DOWNLOAD').path);
Future<String?> getDownloadsPath() {
return Future.value(xdg.getUserDirectory('DOWNLOAD')?.path);
}
}
12 changes: 6 additions & 6 deletions packages/path_provider/path_provider_linux/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: path_provider_linux
description: linux implementation of the path_provider plugin
version: 0.1.1+3
version: 0.2.0-nullsafety
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider_linux

flutter:
Expand All @@ -11,17 +11,17 @@ flutter:
pluginClass: none

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

dependencies:
path: ^1.6.4
xdg_directories: ^0.1.0
path_provider_platform_interface: ^1.0.1
path: ^1.8.0-nullsafety.3
xdg_directories: ^0.2.0-nullsafety.1
path_provider_platform_interface: ^2.0.0-nullsafety
flutter:
sdk: flutter

dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.8.0
pedantic: ^1.10.0-nullsafety.3

0 comments on commit 714913e

Please sign in to comment.