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

Migrate path_provider to null safety. #3460

Merged
merged 7 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all 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/path_provider/path_provider/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.0-nullsafety

* Migrate to null safety.

## 1.6.28

* Drop unused UUID dependency for tests.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Copyright 2019 The Flutter Authors. 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 'package:flutter_test/flutter_test.dart';
import 'package:path_provider/path_provider.dart';
import 'package:integration_test/integration_test.dart';
Expand Down
40 changes: 23 additions & 17 deletions packages/path_provider/path_provider/lib/path_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ PathProviderPlatform get _platform {
/// On iOS, this uses the `NSCachesDirectory` API.
///
/// On Android, this uses the `getCacheDir` API on the context.
Future<Directory> getTemporaryDirectory() async {
final String path = await _platform.getTemporaryPath();
Future<Directory?> getTemporaryDirectory() async {
final String? path = await _platform.getTemporaryPath();
if (path == null) {
return null;
}
Expand All @@ -69,8 +69,8 @@ Future<Directory> getTemporaryDirectory() async {
/// If this directory does not exist, it is created automatically.
///
/// On Android, this function uses the `getFilesDir` API on the context.
Future<Directory> getApplicationSupportDirectory() async {
final String path = await _platform.getApplicationSupportPath();
Future<Directory?> getApplicationSupportDirectory() async {
final String? path = await _platform.getApplicationSupportPath();
if (path == null) {
return null;
}
Expand All @@ -83,8 +83,8 @@ Future<Directory> getApplicationSupportDirectory() async {
///
/// On Android, this function throws an [UnsupportedError] as no equivalent
/// path exists.
Future<Directory> getLibraryDirectory() async {
final String path = await _platform.getLibraryPath();
Future<Directory?> getLibraryDirectory() async {
final String? path = await _platform.getLibraryPath();
if (path == null) {
return null;
}
Expand All @@ -100,8 +100,8 @@ Future<Directory> getLibraryDirectory() async {
/// On Android, this uses the `getDataDirectory` API on the context. Consider
/// using [getExternalStorageDirectory] instead if data is intended to be visible
/// to the user.
Future<Directory> getApplicationDocumentsDirectory() async {
final String path = await _platform.getApplicationDocumentsPath();
Future<Directory?> getApplicationDocumentsDirectory() async {
final String? path = await _platform.getApplicationDocumentsPath();
if (path == null) {
return null;
}
Expand All @@ -116,8 +116,8 @@ Future<Directory> getApplicationDocumentsDirectory() async {
/// to access outside the app's sandbox.
///
/// On Android this uses the `getExternalFilesDir(null)`.
Future<Directory> getExternalStorageDirectory() async {
final String path = await _platform.getExternalStoragePath();
Future<Directory?> getExternalStorageDirectory() async {
final String? path = await _platform.getExternalStoragePath();
if (path == null) {
return null;
}
Expand All @@ -137,8 +137,11 @@ Future<Directory> getExternalStorageDirectory() async {
///
/// On Android this returns Context.getExternalCacheDirs() or
/// Context.getExternalCacheDir() on API levels below 19.
Future<List<Directory>> getExternalCacheDirectories() async {
final List<String> paths = await _platform.getExternalCachePaths();
Future<List<Directory>?> getExternalCacheDirectories() async {
final List<String>? paths = await _platform.getExternalCachePaths();
if (paths == null) {
return null;
}

return paths.map((String path) => Directory(path)).toList();
}
Expand All @@ -155,13 +158,16 @@ Future<List<Directory>> getExternalCacheDirectories() async {
///
/// On Android this returns Context.getExternalFilesDirs(String type) or
/// Context.getExternalFilesDir(String type) on API levels below 19.
Future<List<Directory>> getExternalStorageDirectories({
Future<List<Directory>?> getExternalStorageDirectories({
/// Optional parameter. See [StorageDirectory] for more informations on
/// how this type translates to Android storage directories.
StorageDirectory type,
StorageDirectory? type,
}) async {
final List<String> paths =
final List<String>? paths =
await _platform.getExternalStoragePaths(type: type);
if (paths == null) {
return null;
}

return paths.map((String path) => Directory(path)).toList();
}
Expand All @@ -171,8 +177,8 @@ Future<List<Directory>> getExternalStorageDirectories({
///
/// On Android and on iOS, this function throws an [UnsupportedError] as no equivalent
/// path exists.
Future<Directory> getDownloadsDirectory() async {
final String path = await _platform.getDownloadsPath();
Future<Directory?> getDownloadsDirectory() async {
final String? path = await _platform.getDownloadsPath();
if (path == null) {
return null;
}
Expand Down
18 changes: 9 additions & 9 deletions packages/path_provider/path_provider/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: path_provider
description: Flutter plugin for getting commonly used locations on host platform file systems, such as the temp and app data directories.
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider
version: 1.6.28
version: 2.0.0-nullsafety

flutter:
plugin:
Expand All @@ -21,10 +21,10 @@ flutter:
dependencies:
flutter:
sdk: flutter
path_provider_platform_interface: ^1.0.1
path_provider_macos: ^0.0.4
path_provider_linux: ^0.0.1
path_provider_windows: ^0.0.4
path_provider_platform_interface: ^2.0.0-nullsafety
path_provider_macos: ^0.0.5-nullsafety
path_provider_linux: ^0.2.0-nullsafety
path_provider_windows: ^0.1.0-nullsafety

dev_dependencies:
integration_test:
Expand All @@ -33,10 +33,10 @@ dev_dependencies:
sdk: flutter
flutter_driver:
sdk: flutter
pedantic: ^1.8.0
mockito: ^4.1.1
plugin_platform_interface: ^1.0.0
pedantic: ^1.10.0-nullsafety
mockito: ^5.0.0-nullsafety.0
plugin_platform_interface: ^1.1.0-nullsafety

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.12.0-0 <3.0.0"
flutter: ">=1.12.13+hotfix.5"
38 changes: 19 additions & 19 deletions packages/path_provider/path_provider/test/path_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,45 @@ void main() {
});

test('getTemporaryDirectory', () async {
Directory result = await getTemporaryDirectory();
expect(result.path, kTemporaryPath);
Directory? result = await getTemporaryDirectory();
expect(result?.path, kTemporaryPath);
});

test('getApplicationSupportDirectory', () async {
Directory result = await getApplicationSupportDirectory();
expect(result.path, kApplicationSupportPath);
Directory? result = await getApplicationSupportDirectory();
expect(result?.path, kApplicationSupportPath);
});

test('getLibraryDirectory', () async {
Directory result = await getLibraryDirectory();
expect(result.path, kLibraryPath);
Directory? result = await getLibraryDirectory();
expect(result?.path, kLibraryPath);
});

test('getApplicationDocumentsDirectory', () async {
Directory result = await getApplicationDocumentsDirectory();
expect(result.path, kApplicationDocumentsPath);
Directory? result = await getApplicationDocumentsDirectory();
expect(result?.path, kApplicationDocumentsPath);
});

test('getExternalStorageDirectory', () async {
Directory result = await getExternalStorageDirectory();
expect(result.path, kExternalStoragePath);
Directory? result = await getExternalStorageDirectory();
expect(result?.path, kExternalStoragePath);
});

test('getExternalCacheDirectories', () async {
List<Directory> result = await getExternalCacheDirectories();
expect(result.length, 1);
expect(result.first.path, kExternalCachePath);
List<Directory>? result = await getExternalCacheDirectories();
expect(result?.length, 1);
expect(result?.first.path, kExternalCachePath);
});

test('getExternalStorageDirectories', () async {
List<Directory> result = await getExternalStorageDirectories();
expect(result.length, 1);
expect(result.first.path, kExternalStoragePath);
List<Directory>? result = await getExternalStorageDirectories();
expect(result?.length, 1);
expect(result?.first.path, kExternalStoragePath);
});

test('getDownloadsDirectory', () async {
Directory result = await getDownloadsDirectory();
expect(result.path, kDownloadsPath);
Directory? result = await getDownloadsDirectory();
expect(result?.path, kDownloadsPath);
});
});
}
Expand Down Expand Up @@ -99,7 +99,7 @@ class MockPathProviderPlatform extends Mock
}

Future<List<String>> getExternalStoragePaths({
StorageDirectory type,
StorageDirectory? type,
}) async {
return <String>[kExternalStoragePath];
}
Expand Down