Skip to content

Commit a8ea4d6

Browse files
authored
[path_provider_platform_interface] Add getApplicationCachePath() (flutter#4614)
Platform interface changes split out from flutter#4483. /cc @stuartmorgan
1 parent 9e21922 commit a8ea4d6

File tree

6 files changed

+58
-2
lines changed

6 files changed

+58
-2
lines changed

packages/path_provider/path_provider_platform_interface/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 2.1.0
22

3+
* Adds getApplicationCachePath() for storing app-specific cache files.
34
* Updates minimum supported SDK version to Flutter 3.3/Dart 2.18.
45
* Aligns Dart and Flutter SDK constraints.
56

packages/path_provider/path_provider_platform_interface/lib/path_provider_platform_interface.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ abstract class PathProviderPlatform extends PlatformInterface {
6262
'getApplicationDocumentsPath() has not been implemented.');
6363
}
6464

65+
/// Path to a directory where application specific cache data can be stored.
66+
Future<String?> getApplicationCachePath() {
67+
throw UnimplementedError(
68+
'getApplicationCachePath() has not been implemented.');
69+
}
70+
6571
/// Path to a directory where the application may access top level storage.
6672
/// The current operating system should be determined before issuing this
6773
/// function call, as this functionality is only available on Android.

packages/path_provider/path_provider_platform_interface/lib/src/method_channel_path_provider.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class MethodChannelPathProvider extends PathProviderPlatform {
5252
.invokeMethod<String>('getApplicationDocumentsDirectory');
5353
}
5454

55+
@override
56+
Future<String?> getApplicationCachePath() {
57+
return methodChannel.invokeMethod<String>('getApplicationCacheDirectory');
58+
}
59+
5560
@override
5661
Future<String?> getExternalStoragePath() {
5762
if (!_platform.isAndroid) {

packages/path_provider/path_provider_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/path_provider
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.0.6
7+
version: 2.1.0
88

99
environment:
1010
sdk: ">=2.18.0 <4.0.0"

packages/path_provider/path_provider_platform_interface/test/method_channel_path_provider_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void main() {
1414
const String kApplicationSupportPath = 'applicationSupportPath';
1515
const String kLibraryPath = 'libraryPath';
1616
const String kApplicationDocumentsPath = 'applicationDocumentsPath';
17+
const String kApplicationCachePath = 'applicationCachePath';
1718
const String kExternalCachePaths = 'externalCachePaths';
1819
const String kExternalStoragePaths = 'externalStoragePaths';
1920
const String kDownloadsPath = 'downloadsPath';
@@ -39,6 +40,8 @@ void main() {
3940
return kLibraryPath;
4041
case 'getApplicationDocumentsDirectory':
4142
return kApplicationDocumentsPath;
43+
case 'getApplicationCacheDirectory':
44+
return kApplicationCachePath;
4245
case 'getExternalStorageDirectories':
4346
return <String>[kExternalStoragePaths];
4447
case 'getExternalCacheDirectories':
@@ -126,6 +129,18 @@ void main() {
126129
expect(path, kApplicationDocumentsPath);
127130
});
128131

132+
test('getApplicationCachePath succeeds', () async {
133+
final String? result =
134+
await methodChannelPathProvider.getApplicationCachePath();
135+
expect(
136+
log,
137+
<Matcher>[
138+
isMethodCall('getApplicationCacheDirectory', arguments: null)
139+
],
140+
);
141+
expect(result, kApplicationCachePath);
142+
});
143+
129144
test('getExternalCachePaths android succeeds', () async {
130145
final List<String>? result =
131146
await methodChannelPathProvider.getExternalCachePaths();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2013 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+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
7+
import 'package:path_provider_platform_interface/src/method_channel_path_provider.dart';
8+
9+
void main() {
10+
TestWidgetsFlutterBinding.ensureInitialized();
11+
12+
group('$PathProviderPlatform', () {
13+
test('$MethodChannelPathProvider is the default instance', () {
14+
expect(PathProviderPlatform.instance, isA<MethodChannelPathProvider>());
15+
});
16+
17+
test('getApplicationCachePath throws unimplemented error', () {
18+
final ExtendsPathProviderPlatform pathProviderPlatform =
19+
ExtendsPathProviderPlatform();
20+
21+
expect(
22+
() => pathProviderPlatform.getApplicationCachePath(),
23+
throwsUnimplementedError,
24+
);
25+
});
26+
});
27+
}
28+
29+
class ExtendsPathProviderPlatform extends PathProviderPlatform {}

0 commit comments

Comments
 (0)