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

Commit 7f5696c

Browse files
Migrate path_provider to null safety. (#3460)
1 parent 60fa979 commit 7f5696c

File tree

5 files changed

+61
-45
lines changed

5 files changed

+61
-45
lines changed

packages/path_provider/path_provider/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.0-nullsafety
2+
3+
* Migrate to null safety.
4+
15
## 1.6.28
26

37
* Drop unused UUID dependency for tests.

packages/path_provider/path_provider/integration_test/path_provider_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Copyright 2019 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// @dart=2.9
6+
17
import 'package:flutter_test/flutter_test.dart';
28
import 'package:path_provider/path_provider.dart';
39
import 'package:integration_test/integration_test.dart';

packages/path_provider/path_provider/lib/path_provider.dart

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ PathProviderPlatform get _platform {
5151
/// On iOS, this uses the `NSCachesDirectory` API.
5252
///
5353
/// On Android, this uses the `getCacheDir` API on the context.
54-
Future<Directory> getTemporaryDirectory() async {
55-
final String path = await _platform.getTemporaryPath();
54+
Future<Directory?> getTemporaryDirectory() async {
55+
final String? path = await _platform.getTemporaryPath();
5656
if (path == null) {
5757
return null;
5858
}
@@ -69,8 +69,8 @@ Future<Directory> getTemporaryDirectory() async {
6969
/// If this directory does not exist, it is created automatically.
7070
///
7171
/// On Android, this function uses the `getFilesDir` API on the context.
72-
Future<Directory> getApplicationSupportDirectory() async {
73-
final String path = await _platform.getApplicationSupportPath();
72+
Future<Directory?> getApplicationSupportDirectory() async {
73+
final String? path = await _platform.getApplicationSupportPath();
7474
if (path == null) {
7575
return null;
7676
}
@@ -83,8 +83,8 @@ Future<Directory> getApplicationSupportDirectory() async {
8383
///
8484
/// On Android, this function throws an [UnsupportedError] as no equivalent
8585
/// path exists.
86-
Future<Directory> getLibraryDirectory() async {
87-
final String path = await _platform.getLibraryPath();
86+
Future<Directory?> getLibraryDirectory() async {
87+
final String? path = await _platform.getLibraryPath();
8888
if (path == null) {
8989
return null;
9090
}
@@ -100,8 +100,8 @@ Future<Directory> getLibraryDirectory() async {
100100
/// On Android, this uses the `getDataDirectory` API on the context. Consider
101101
/// using [getExternalStorageDirectory] instead if data is intended to be visible
102102
/// to the user.
103-
Future<Directory> getApplicationDocumentsDirectory() async {
104-
final String path = await _platform.getApplicationDocumentsPath();
103+
Future<Directory?> getApplicationDocumentsDirectory() async {
104+
final String? path = await _platform.getApplicationDocumentsPath();
105105
if (path == null) {
106106
return null;
107107
}
@@ -116,8 +116,8 @@ Future<Directory> getApplicationDocumentsDirectory() async {
116116
/// to access outside the app's sandbox.
117117
///
118118
/// On Android this uses the `getExternalFilesDir(null)`.
119-
Future<Directory> getExternalStorageDirectory() async {
120-
final String path = await _platform.getExternalStoragePath();
119+
Future<Directory?> getExternalStorageDirectory() async {
120+
final String? path = await _platform.getExternalStoragePath();
121121
if (path == null) {
122122
return null;
123123
}
@@ -137,8 +137,11 @@ Future<Directory> getExternalStorageDirectory() async {
137137
///
138138
/// On Android this returns Context.getExternalCacheDirs() or
139139
/// Context.getExternalCacheDir() on API levels below 19.
140-
Future<List<Directory>> getExternalCacheDirectories() async {
141-
final List<String> paths = await _platform.getExternalCachePaths();
140+
Future<List<Directory>?> getExternalCacheDirectories() async {
141+
final List<String>? paths = await _platform.getExternalCachePaths();
142+
if (paths == null) {
143+
return null;
144+
}
142145

143146
return paths.map((String path) => Directory(path)).toList();
144147
}
@@ -155,13 +158,16 @@ Future<List<Directory>> getExternalCacheDirectories() async {
155158
///
156159
/// On Android this returns Context.getExternalFilesDirs(String type) or
157160
/// Context.getExternalFilesDir(String type) on API levels below 19.
158-
Future<List<Directory>> getExternalStorageDirectories({
161+
Future<List<Directory>?> getExternalStorageDirectories({
159162
/// Optional parameter. See [StorageDirectory] for more informations on
160163
/// how this type translates to Android storage directories.
161-
StorageDirectory type,
164+
StorageDirectory? type,
162165
}) async {
163-
final List<String> paths =
166+
final List<String>? paths =
164167
await _platform.getExternalStoragePaths(type: type);
168+
if (paths == null) {
169+
return null;
170+
}
165171

166172
return paths.map((String path) => Directory(path)).toList();
167173
}
@@ -171,8 +177,8 @@ Future<List<Directory>> getExternalStorageDirectories({
171177
///
172178
/// On Android and on iOS, this function throws an [UnsupportedError] as no equivalent
173179
/// path exists.
174-
Future<Directory> getDownloadsDirectory() async {
175-
final String path = await _platform.getDownloadsPath();
180+
Future<Directory?> getDownloadsDirectory() async {
181+
final String? path = await _platform.getDownloadsPath();
176182
if (path == null) {
177183
return null;
178184
}

packages/path_provider/path_provider/pubspec.yaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: path_provider
22
description: Flutter plugin for getting commonly used locations on host platform file systems, such as the temp and app data directories.
33
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider
4-
version: 1.6.28
4+
version: 2.0.0-nullsafety
55

66
flutter:
77
plugin:
@@ -21,10 +21,10 @@ flutter:
2121
dependencies:
2222
flutter:
2323
sdk: flutter
24-
path_provider_platform_interface: ^1.0.1
25-
path_provider_macos: ^0.0.4
26-
path_provider_linux: ^0.0.1
27-
path_provider_windows: ^0.0.4
24+
path_provider_platform_interface: ^2.0.0-nullsafety
25+
path_provider_macos: ^0.0.5-nullsafety
26+
path_provider_linux: ^0.2.0-nullsafety
27+
path_provider_windows: ^0.1.0-nullsafety
2828

2929
dev_dependencies:
3030
integration_test:
@@ -33,10 +33,10 @@ dev_dependencies:
3333
sdk: flutter
3434
flutter_driver:
3535
sdk: flutter
36-
pedantic: ^1.8.0
37-
mockito: ^4.1.1
38-
plugin_platform_interface: ^1.0.0
36+
pedantic: ^1.10.0-nullsafety
37+
mockito: ^5.0.0-nullsafety.0
38+
plugin_platform_interface: ^1.1.0-nullsafety
3939

4040
environment:
41-
sdk: ">=2.1.0 <3.0.0"
41+
sdk: ">=2.12.0-0 <3.0.0"
4242
flutter: ">=1.12.13+hotfix.5"

packages/path_provider/path_provider/test/path_provider_test.dart

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,45 @@ void main() {
2828
});
2929

3030
test('getTemporaryDirectory', () async {
31-
Directory result = await getTemporaryDirectory();
32-
expect(result.path, kTemporaryPath);
31+
Directory? result = await getTemporaryDirectory();
32+
expect(result?.path, kTemporaryPath);
3333
});
3434

3535
test('getApplicationSupportDirectory', () async {
36-
Directory result = await getApplicationSupportDirectory();
37-
expect(result.path, kApplicationSupportPath);
36+
Directory? result = await getApplicationSupportDirectory();
37+
expect(result?.path, kApplicationSupportPath);
3838
});
3939

4040
test('getLibraryDirectory', () async {
41-
Directory result = await getLibraryDirectory();
42-
expect(result.path, kLibraryPath);
41+
Directory? result = await getLibraryDirectory();
42+
expect(result?.path, kLibraryPath);
4343
});
4444

4545
test('getApplicationDocumentsDirectory', () async {
46-
Directory result = await getApplicationDocumentsDirectory();
47-
expect(result.path, kApplicationDocumentsPath);
46+
Directory? result = await getApplicationDocumentsDirectory();
47+
expect(result?.path, kApplicationDocumentsPath);
4848
});
4949

5050
test('getExternalStorageDirectory', () async {
51-
Directory result = await getExternalStorageDirectory();
52-
expect(result.path, kExternalStoragePath);
51+
Directory? result = await getExternalStorageDirectory();
52+
expect(result?.path, kExternalStoragePath);
5353
});
5454

5555
test('getExternalCacheDirectories', () async {
56-
List<Directory> result = await getExternalCacheDirectories();
57-
expect(result.length, 1);
58-
expect(result.first.path, kExternalCachePath);
56+
List<Directory>? result = await getExternalCacheDirectories();
57+
expect(result?.length, 1);
58+
expect(result?.first.path, kExternalCachePath);
5959
});
6060

6161
test('getExternalStorageDirectories', () async {
62-
List<Directory> result = await getExternalStorageDirectories();
63-
expect(result.length, 1);
64-
expect(result.first.path, kExternalStoragePath);
62+
List<Directory>? result = await getExternalStorageDirectories();
63+
expect(result?.length, 1);
64+
expect(result?.first.path, kExternalStoragePath);
6565
});
6666

6767
test('getDownloadsDirectory', () async {
68-
Directory result = await getDownloadsDirectory();
69-
expect(result.path, kDownloadsPath);
68+
Directory? result = await getDownloadsDirectory();
69+
expect(result?.path, kDownloadsPath);
7070
});
7171
});
7272
}
@@ -99,7 +99,7 @@ class MockPathProviderPlatform extends Mock
9999
}
100100

101101
Future<List<String>> getExternalStoragePaths({
102-
StorageDirectory type,
102+
StorageDirectory? type,
103103
}) async {
104104
return <String>[kExternalStoragePath];
105105
}

0 commit comments

Comments
 (0)