mirror of
https://github.com/flutter/packages.git
synced 2025-06-30 14:47:22 +08:00
[path_provider] Add getApplicationCachePath() - implementations (#4619)
Platform implementations split out from #4483.
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
## NEXT
|
||||
## 2.1.0
|
||||
|
||||
* Adds getApplicationCachePath() for storing app-specific cache files.
|
||||
* Updates minimum supported SDK version to Flutter 3.3/Dart 2.18.
|
||||
|
||||
## 2.0.27
|
||||
|
@ -1,7 +1,7 @@
|
||||
// Copyright 2013 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.
|
||||
// Autogenerated from Pigeon (v9.2.4), do not edit directly.
|
||||
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
|
||||
// See also: https://pub.dev/packages/pigeon
|
||||
|
||||
package io.flutter.plugins.pathprovider;
|
||||
@ -84,6 +84,9 @@ public class Messages {
|
||||
@Nullable
|
||||
String getApplicationDocumentsPath();
|
||||
|
||||
@Nullable
|
||||
String getApplicationCachePath();
|
||||
|
||||
@Nullable
|
||||
String getExternalStoragePath();
|
||||
|
||||
@ -176,6 +179,31 @@ public class Messages {
|
||||
channel.setMessageHandler(null);
|
||||
}
|
||||
}
|
||||
{
|
||||
BinaryMessenger.TaskQueue taskQueue = binaryMessenger.makeBackgroundTaskQueue();
|
||||
BasicMessageChannel<Object> channel =
|
||||
new BasicMessageChannel<>(
|
||||
binaryMessenger,
|
||||
"dev.flutter.pigeon.PathProviderApi.getApplicationCachePath",
|
||||
getCodec(),
|
||||
taskQueue);
|
||||
if (api != null) {
|
||||
channel.setMessageHandler(
|
||||
(message, reply) -> {
|
||||
ArrayList<Object> wrapped = new ArrayList<Object>();
|
||||
try {
|
||||
String output = api.getApplicationCachePath();
|
||||
wrapped.add(0, output);
|
||||
} catch (Throwable exception) {
|
||||
ArrayList<Object> wrappedError = wrapError(exception);
|
||||
wrapped = wrappedError;
|
||||
}
|
||||
reply.reply(wrapped);
|
||||
});
|
||||
} else {
|
||||
channel.setMessageHandler(null);
|
||||
}
|
||||
}
|
||||
{
|
||||
BinaryMessenger.TaskQueue taskQueue = binaryMessenger.makeBackgroundTaskQueue();
|
||||
BasicMessageChannel<Object> channel =
|
||||
|
@ -66,6 +66,11 @@ public class PathProviderPlugin implements FlutterPlugin, PathProviderApi {
|
||||
return getPathProviderApplicationDocumentsDirectory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getApplicationCachePath() {
|
||||
return context.getCacheDir().getPath();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String getExternalStoragePath() {
|
||||
return getPathProviderStorageDirectory();
|
||||
|
@ -28,6 +28,12 @@ void main() {
|
||||
_verifySampleFile(result, 'applicationSupport');
|
||||
});
|
||||
|
||||
testWidgets('getApplicationCacheDirectory', (WidgetTester tester) async {
|
||||
final PathProviderPlatform provider = PathProviderPlatform.instance;
|
||||
final String? result = await provider.getApplicationCachePath();
|
||||
_verifySampleFile(result, 'applicationCache');
|
||||
});
|
||||
|
||||
testWidgets('getLibraryDirectory', (WidgetTester tester) async {
|
||||
final PathProviderPlatform provider = PathProviderPlatform.instance;
|
||||
expect(() => provider.getLibraryPath(),
|
||||
|
@ -39,6 +39,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
Future<String?>? _tempDirectory;
|
||||
Future<String?>? _appSupportDirectory;
|
||||
Future<String?>? _appDocumentsDirectory;
|
||||
Future<String?>? _appCacheDirectory;
|
||||
Future<String?>? _externalDocumentsDirectory;
|
||||
Future<List<String>?>? _externalStorageDirectories;
|
||||
Future<List<String>?>? _externalCacheDirectories;
|
||||
@ -92,6 +93,12 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
});
|
||||
}
|
||||
|
||||
void _requestAppCacheDirectory() {
|
||||
setState(() {
|
||||
_appCacheDirectory = provider.getApplicationCachePath();
|
||||
});
|
||||
}
|
||||
|
||||
void _requestExternalStorageDirectory() {
|
||||
setState(() {
|
||||
_externalDocumentsDirectory = provider.getExternalStoragePath();
|
||||
@ -147,6 +154,15 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
),
|
||||
FutureBuilder<String?>(
|
||||
future: _appSupportDirectory, builder: _buildDirectory),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: ElevatedButton(
|
||||
onPressed: _requestAppCacheDirectory,
|
||||
child: const Text('Get Application Cache Directory'),
|
||||
),
|
||||
),
|
||||
FutureBuilder<String?>(
|
||||
future: _appCacheDirectory, builder: _buildDirectory),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: ElevatedButton(
|
||||
|
@ -16,7 +16,7 @@ dependencies:
|
||||
# The example app is bundled with the plugin so we use a path dependency on
|
||||
# the parent directory to use the current plugin's version.
|
||||
path: ../
|
||||
path_provider_platform_interface: ^2.0.0
|
||||
path_provider_platform_interface: ^2.1.0
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
@ -1,7 +1,7 @@
|
||||
// Copyright 2013 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.
|
||||
// Autogenerated from Pigeon (v9.2.4), do not edit directly.
|
||||
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
|
||||
// See also: https://pub.dev/packages/pigeon
|
||||
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import
|
||||
|
||||
@ -98,6 +98,27 @@ class PathProviderApi {
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> getApplicationCachePath() async {
|
||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||
'dev.flutter.pigeon.PathProviderApi.getApplicationCachePath', codec,
|
||||
binaryMessenger: _binaryMessenger);
|
||||
final List<Object?>? replyList = await channel.send(null) as List<Object?>?;
|
||||
if (replyList == null) {
|
||||
throw PlatformException(
|
||||
code: 'channel-error',
|
||||
message: 'Unable to establish connection on channel.',
|
||||
);
|
||||
} else if (replyList.length > 1) {
|
||||
throw PlatformException(
|
||||
code: replyList[0]! as String,
|
||||
message: replyList[1] as String?,
|
||||
details: replyList[2],
|
||||
);
|
||||
} else {
|
||||
return (replyList[0] as String?);
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> getExternalStoragePath() async {
|
||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||
'dev.flutter.pigeon.PathProviderApi.getExternalStoragePath', codec,
|
||||
|
@ -62,6 +62,11 @@ class PathProviderAndroid extends PathProviderPlatform {
|
||||
return _api.getApplicationDocumentsPath();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getApplicationCachePath() {
|
||||
return _api.getApplicationCachePath();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getExternalStoragePath() {
|
||||
return _api.getExternalStoragePath();
|
||||
|
@ -36,6 +36,8 @@ abstract class PathProviderApi {
|
||||
@TaskQueue(type: TaskQueueType.serialBackgroundThread)
|
||||
String? getApplicationDocumentsPath();
|
||||
@TaskQueue(type: TaskQueueType.serialBackgroundThread)
|
||||
String? getApplicationCachePath();
|
||||
@TaskQueue(type: TaskQueueType.serialBackgroundThread)
|
||||
String? getExternalStoragePath();
|
||||
@TaskQueue(type: TaskQueueType.serialBackgroundThread)
|
||||
List<String?> getExternalCachePaths();
|
||||
|
@ -2,7 +2,7 @@ name: path_provider_android
|
||||
description: Android implementation of the path_provider plugin.
|
||||
repository: https://github.com/flutter/packages/tree/main/packages/path_provider/path_provider_android
|
||||
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
|
||||
version: 2.0.27
|
||||
version: 2.1.0
|
||||
|
||||
environment:
|
||||
sdk: ">=2.18.0 <4.0.0"
|
||||
@ -20,7 +20,7 @@ flutter:
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
path_provider_platform_interface: ^2.0.1
|
||||
path_provider_platform_interface: ^2.1.0
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
@ -1,7 +1,7 @@
|
||||
// Copyright 2013 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.
|
||||
// Autogenerated from Pigeon (v9.2.4), do not edit directly.
|
||||
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
|
||||
// See also: https://pub.dev/packages/pigeon
|
||||
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import
|
||||
// ignore_for_file: avoid_relative_lib_imports
|
||||
@ -24,6 +24,8 @@ abstract class TestPathProviderApi {
|
||||
|
||||
String? getApplicationDocumentsPath();
|
||||
|
||||
String? getApplicationCachePath();
|
||||
|
||||
String? getExternalStoragePath();
|
||||
|
||||
List<String?> getExternalCachePaths();
|
||||
@ -84,6 +86,23 @@ abstract class TestPathProviderApi {
|
||||
});
|
||||
}
|
||||
}
|
||||
{
|
||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||
'dev.flutter.pigeon.PathProviderApi.getApplicationCachePath', codec,
|
||||
binaryMessenger: binaryMessenger);
|
||||
if (api == null) {
|
||||
_testBinaryMessengerBinding!.defaultBinaryMessenger
|
||||
.setMockDecodedMessageHandler<Object?>(channel, null);
|
||||
} else {
|
||||
_testBinaryMessengerBinding!.defaultBinaryMessenger
|
||||
.setMockDecodedMessageHandler<Object?>(channel,
|
||||
(Object? message) async {
|
||||
// ignore message
|
||||
final String? output = api.getApplicationCachePath();
|
||||
return <Object?>[output];
|
||||
});
|
||||
}
|
||||
}
|
||||
{
|
||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||
'dev.flutter.pigeon.PathProviderApi.getExternalStoragePath', codec,
|
||||
|
@ -12,6 +12,7 @@ const String kTemporaryPath = 'temporaryPath';
|
||||
const String kApplicationSupportPath = 'applicationSupportPath';
|
||||
const String kLibraryPath = 'libraryPath';
|
||||
const String kApplicationDocumentsPath = 'applicationDocumentsPath';
|
||||
const String kApplicationCachePath = 'applicationCachePath';
|
||||
const String kExternalCachePaths = 'externalCachePaths';
|
||||
const String kExternalStoragePaths = 'externalStoragePaths';
|
||||
const String kDownloadsPath = 'downloadsPath';
|
||||
@ -23,6 +24,9 @@ class _Api implements TestPathProviderApi {
|
||||
@override
|
||||
String? getApplicationSupportPath() => kApplicationSupportPath;
|
||||
|
||||
@override
|
||||
String? getApplicationCachePath() => kApplicationCachePath;
|
||||
|
||||
@override
|
||||
List<String?> getExternalCachePaths() => <String>[kExternalCachePaths];
|
||||
|
||||
@ -58,6 +62,11 @@ void main() {
|
||||
expect(path, kApplicationSupportPath);
|
||||
});
|
||||
|
||||
test('getApplicationCachePath', () async {
|
||||
final String? path = await pathProvider.getApplicationCachePath();
|
||||
expect(path, kApplicationCachePath);
|
||||
});
|
||||
|
||||
test('getLibraryPath fails', () async {
|
||||
try {
|
||||
await pathProvider.getLibraryPath();
|
||||
|
@ -1,3 +1,7 @@
|
||||
## 2.3.0
|
||||
|
||||
* Adds getApplicationCachePath() for storing app-specific cache files.
|
||||
|
||||
## 2.2.4
|
||||
|
||||
* Updates to the latest version of `pigeon`.
|
||||
|
@ -25,12 +25,12 @@ public class PathProviderPlugin: NSObject, FlutterPlugin, PathProviderApi {
|
||||
func getDirectoryPath(type: DirectoryType) -> String? {
|
||||
var path = getDirectory(ofType: fileManagerDirectoryForType(type))
|
||||
#if os(macOS)
|
||||
// In a non-sandboxed app, this is a shared directory where applications are
|
||||
// In a non-sandboxed app, these are shared directories where applications are
|
||||
// expected to use its bundle ID as a subdirectory. (For non-sandboxed apps,
|
||||
// adding the extra path is harmless).
|
||||
// This is not done for iOS, for compatibility with older versions of the
|
||||
// plugin.
|
||||
if type == .applicationSupport {
|
||||
if type == .applicationSupport || type == .applicationCache {
|
||||
if let basePath = path {
|
||||
let basePathURL = URL.init(fileURLWithPath: basePath)
|
||||
path = basePathURL.appendingPathComponent(Bundle.main.bundleIdentifier!).path
|
||||
@ -49,6 +49,8 @@ public class PathProviderPlugin: NSObject, FlutterPlugin, PathProviderApi {
|
||||
/// Returns the FileManager constant corresponding to the given type.
|
||||
private func fileManagerDirectoryForType(_ type: DirectoryType) -> FileManager.SearchPathDirectory {
|
||||
switch type {
|
||||
case .applicationCache:
|
||||
return FileManager.SearchPathDirectory.cachesDirectory
|
||||
case .applicationDocuments:
|
||||
return FileManager.SearchPathDirectory.documentDirectory
|
||||
case .applicationSupport:
|
||||
|
@ -44,6 +44,7 @@ enum DirectoryType: Int {
|
||||
case downloads = 2
|
||||
case library = 3
|
||||
case temp = 4
|
||||
case applicationCache = 5
|
||||
}
|
||||
/// Generated protocol from Pigeon that represents a handler of messages from Flutter.
|
||||
protocol PathProviderApi {
|
||||
|
@ -29,6 +29,12 @@ void main() {
|
||||
_verifySampleFile(result, 'applicationSupport');
|
||||
});
|
||||
|
||||
testWidgets('getApplicationCacheDirectory', (WidgetTester tester) async {
|
||||
final PathProviderPlatform provider = PathProviderPlatform.instance;
|
||||
final String? result = await provider.getApplicationCachePath();
|
||||
_verifySampleFile(result, 'applicationCache');
|
||||
});
|
||||
|
||||
testWidgets('getLibraryDirectory', (WidgetTester tester) async {
|
||||
final PathProviderPlatform provider = PathProviderPlatform.instance;
|
||||
final String? result = await provider.getLibraryPath();
|
||||
|
@ -27,6 +27,7 @@ class _MyAppState extends State<MyApp> {
|
||||
String? _appSupportDirectory = 'Unknown';
|
||||
String? _documentsDirectory = 'Unknown';
|
||||
String? _containerDirectory = 'Unknown';
|
||||
String? _cacheDirectory = 'Unknown';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@ -42,6 +43,7 @@ class _MyAppState extends State<MyApp> {
|
||||
String? libraryDirectory;
|
||||
String? documentsDirectory;
|
||||
String? containerDirectory;
|
||||
String? cacheDirectory;
|
||||
final PathProviderPlatform provider = PathProviderPlatform.instance;
|
||||
final PathProviderFoundation providerFoundation = PathProviderFoundation();
|
||||
|
||||
@ -82,6 +84,12 @@ class _MyAppState extends State<MyApp> {
|
||||
'Failed to get app group container directory: $exception';
|
||||
}
|
||||
|
||||
try {
|
||||
cacheDirectory = await provider.getApplicationCachePath();
|
||||
} catch (exception) {
|
||||
cacheDirectory = 'Failed to get cache directory: $exception';
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_tempDirectory = tempDirectory;
|
||||
_downloadsDirectory = downloadsDirectory;
|
||||
@ -89,6 +97,7 @@ class _MyAppState extends State<MyApp> {
|
||||
_appSupportDirectory = appSupportDirectory;
|
||||
_documentsDirectory = documentsDirectory;
|
||||
_containerDirectory = containerDirectory;
|
||||
_cacheDirectory = cacheDirectory;
|
||||
});
|
||||
}
|
||||
|
||||
@ -108,6 +117,7 @@ class _MyAppState extends State<MyApp> {
|
||||
Text('Library Directory: $_libraryDirectory\n'),
|
||||
Text('Application Support Directory: $_appSupportDirectory\n'),
|
||||
Text('App Group Container Directory: $_containerDirectory\n'),
|
||||
Text('Cache Directory: $_cacheDirectory\n'),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
@ -16,7 +16,7 @@ dependencies:
|
||||
# The example app is bundled with the plugin so we use a path dependency on
|
||||
# the parent directory to use the current plugin's version.
|
||||
path: ../
|
||||
path_provider_platform_interface: ^2.0.0
|
||||
path_provider_platform_interface: ^2.1.0
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
@ -17,6 +17,7 @@ enum DirectoryType {
|
||||
downloads,
|
||||
library,
|
||||
temp,
|
||||
applicationCache,
|
||||
}
|
||||
|
||||
class PathProviderApi {
|
||||
|
@ -51,6 +51,18 @@ class PathProviderFoundation extends PathProviderPlatform {
|
||||
return _pathProvider.getDirectoryPath(DirectoryType.applicationDocuments);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getApplicationCachePath() async {
|
||||
final String? path =
|
||||
await _pathProvider.getDirectoryPath(DirectoryType.applicationCache);
|
||||
if (path != null) {
|
||||
// Ensure the directory exists before returning it, for consistency with
|
||||
// other platforms.
|
||||
await Directory(path).create(recursive: true);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getExternalStoragePath() async {
|
||||
throw UnsupportedError(
|
||||
|
@ -16,6 +16,7 @@ enum DirectoryType {
|
||||
downloads,
|
||||
library,
|
||||
temp,
|
||||
applicationCache,
|
||||
}
|
||||
|
||||
@HostApi(dartHostTestHandler: 'TestPathProviderApi')
|
||||
|
@ -2,7 +2,7 @@ name: path_provider_foundation
|
||||
description: iOS and macOS implementation of the path_provider plugin
|
||||
repository: https://github.com/flutter/packages/tree/main/packages/path_provider/path_provider_foundation
|
||||
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
|
||||
version: 2.2.4
|
||||
version: 2.3.0
|
||||
|
||||
environment:
|
||||
sdk: ">=2.18.0 <4.0.0"
|
||||
@ -24,7 +24,7 @@ flutter:
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
path_provider_platform_interface: ^2.0.1
|
||||
path_provider_platform_interface: ^2.1.0
|
||||
|
||||
dev_dependencies:
|
||||
build_runner: ^2.3.2
|
||||
|
@ -98,6 +98,32 @@ void main() {
|
||||
expect(path, applicationDocumentsPath);
|
||||
});
|
||||
|
||||
test('getApplicationCachePath', () async {
|
||||
final PathProviderFoundation pathProvider = PathProviderFoundation();
|
||||
final String applicationCachePath =
|
||||
p.join(testRoot.path, 'application', 'cache', 'path');
|
||||
when(mockApi.getDirectoryPath(DirectoryType.applicationCache))
|
||||
.thenReturn(applicationCachePath);
|
||||
|
||||
final String? path = await pathProvider.getApplicationCachePath();
|
||||
|
||||
verify(mockApi.getDirectoryPath(DirectoryType.applicationCache));
|
||||
expect(path, applicationCachePath);
|
||||
});
|
||||
|
||||
test('getApplicationCachePath creates the directory if necessary',
|
||||
() async {
|
||||
final PathProviderFoundation pathProvider = PathProviderFoundation();
|
||||
final String applicationCachePath =
|
||||
p.join(testRoot.path, 'application', 'cache', 'path');
|
||||
when(mockApi.getDirectoryPath(DirectoryType.applicationCache))
|
||||
.thenReturn(applicationCachePath);
|
||||
|
||||
final String? path = await pathProvider.getApplicationCachePath();
|
||||
|
||||
expect(Directory(path!).existsSync(), isTrue);
|
||||
});
|
||||
|
||||
test('getDownloadsPath', () async {
|
||||
final PathProviderFoundation pathProvider = PathProviderFoundation();
|
||||
final String downloadsPath = p.join(testRoot.path, 'downloads', 'path');
|
||||
|
@ -1,3 +1,7 @@
|
||||
## 2.2.0
|
||||
|
||||
* Adds getApplicationCachePath() for storing app-specific cache files.
|
||||
|
||||
## 2.1.11
|
||||
|
||||
* Removes obsolete null checks on non-nullable values.
|
||||
|
@ -36,6 +36,12 @@ void main() {
|
||||
final String? result = await provider.getApplicationSupportPath();
|
||||
_verifySampleFile(result, 'applicationSupport');
|
||||
});
|
||||
|
||||
testWidgets('getApplicationCacheDirectory', (WidgetTester tester) async {
|
||||
final PathProviderLinux provider = PathProviderLinux();
|
||||
final String? result = await provider.getApplicationCachePath();
|
||||
_verifySampleFile(result, 'applicationCache');
|
||||
});
|
||||
}
|
||||
|
||||
/// Verify a file called [name] in [directoryPath] by recreating it with test
|
||||
|
@ -23,6 +23,7 @@ class _MyAppState extends State<MyApp> {
|
||||
String? _tempDirectory = 'Unknown';
|
||||
String? _downloadsDirectory = 'Unknown';
|
||||
String? _appSupportDirectory = 'Unknown';
|
||||
String? _appCacheDirectory = 'Unknown';
|
||||
String? _documentsDirectory = 'Unknown';
|
||||
final PathProviderLinux _provider = PathProviderLinux();
|
||||
|
||||
@ -37,6 +38,7 @@ class _MyAppState extends State<MyApp> {
|
||||
String? tempDirectory;
|
||||
String? downloadsDirectory;
|
||||
String? appSupportDirectory;
|
||||
String? appCacheDirectory;
|
||||
String? documentsDirectory;
|
||||
// Platform messages may fail, so we use a try/catch PlatformException.
|
||||
try {
|
||||
@ -61,6 +63,12 @@ class _MyAppState extends State<MyApp> {
|
||||
} on PlatformException {
|
||||
appSupportDirectory = 'Failed to get documents directory.';
|
||||
}
|
||||
|
||||
try {
|
||||
appCacheDirectory = await _provider.getApplicationCachePath();
|
||||
} on PlatformException {
|
||||
appCacheDirectory = 'Failed to get cache directory.';
|
||||
}
|
||||
// If the widget was removed from the tree while the asynchronous platform
|
||||
// message was in flight, we want to discard the reply rather than calling
|
||||
// setState to update our non-existent appearance.
|
||||
@ -72,6 +80,7 @@ class _MyAppState extends State<MyApp> {
|
||||
_tempDirectory = tempDirectory;
|
||||
_downloadsDirectory = downloadsDirectory;
|
||||
_appSupportDirectory = appSupportDirectory;
|
||||
_appCacheDirectory = appCacheDirectory;
|
||||
_documentsDirectory = documentsDirectory;
|
||||
});
|
||||
}
|
||||
@ -90,6 +99,7 @@ class _MyAppState extends State<MyApp> {
|
||||
Text('Documents Directory: $_documentsDirectory\n'),
|
||||
Text('Downloads Directory: $_downloadsDirectory\n'),
|
||||
Text('Application Support Directory: $_appSupportDirectory\n'),
|
||||
Text('Application Cache Directory: $_appCacheDirectory\n'),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
@ -71,6 +71,16 @@ class PathProviderLinux extends PathProviderPlatform {
|
||||
return Future<String?>.value(xdg.getUserDirectory('DOCUMENTS')?.path);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getApplicationCachePath() async {
|
||||
final Directory directory =
|
||||
Directory(path.join(xdg.cacheHome.path, await _getId()));
|
||||
if (!directory.existsSync()) {
|
||||
await directory.create(recursive: true);
|
||||
}
|
||||
return directory.path;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getDownloadsPath() {
|
||||
return Future<String?>.value(xdg.getUserDirectory('DOWNLOAD')?.path);
|
||||
|
@ -2,7 +2,7 @@ name: path_provider_linux
|
||||
description: Linux implementation of the path_provider plugin
|
||||
repository: https://github.com/flutter/packages/tree/main/packages/path_provider/path_provider_linux
|
||||
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
|
||||
version: 2.1.11
|
||||
version: 2.2.0
|
||||
|
||||
environment:
|
||||
sdk: ">=2.18.0 <4.0.0"
|
||||
@ -20,7 +20,7 @@ dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
path: ^1.8.0
|
||||
path_provider_platform_interface: ^2.0.0
|
||||
path_provider_platform_interface: ^2.1.0
|
||||
xdg_directories: ">=0.2.0 <2.0.0"
|
||||
|
||||
dev_dependencies:
|
||||
|
@ -57,6 +57,13 @@ void main() {
|
||||
expect(await plugin.getApplicationDocumentsPath(), startsWith('/'));
|
||||
});
|
||||
|
||||
test('getApplicationCachePath', () async {
|
||||
final PathProviderPlatform plugin = PathProviderLinux.private(
|
||||
executableName: 'path_provider_linux_test_binary');
|
||||
expect(await plugin.getApplicationCachePath(),
|
||||
'${xdg.cacheHome.path}/path_provider_linux_test_binary');
|
||||
});
|
||||
|
||||
test('getDownloadsPath', () async {
|
||||
final PathProviderPlatform plugin = PathProviderPlatform.instance;
|
||||
expect(await plugin.getDownloadsPath(), startsWith('/'));
|
||||
|
@ -1,3 +1,7 @@
|
||||
## 2.2.0
|
||||
|
||||
* Adds getApplicationCachePath() for storing app-specific cache files.
|
||||
|
||||
## 2.1.7
|
||||
|
||||
* Adds compatibility with `win32` 5.x.
|
||||
|
@ -28,6 +28,12 @@ void main() {
|
||||
_verifySampleFile(result, 'applicationSupport');
|
||||
});
|
||||
|
||||
testWidgets('getApplicationCacheDirectory', (WidgetTester tester) async {
|
||||
final PathProviderWindows provider = PathProviderWindows();
|
||||
final String? result = await provider.getApplicationCachePath();
|
||||
_verifySampleFile(result, 'applicationCache');
|
||||
});
|
||||
|
||||
testWidgets('getDownloadsDirectory', (WidgetTester tester) async {
|
||||
final PathProviderWindows provider = PathProviderWindows();
|
||||
final String? result = await provider.getDownloadsPath();
|
||||
|
@ -24,6 +24,7 @@ class _MyAppState extends State<MyApp> {
|
||||
String? _downloadsDirectory = 'Unknown';
|
||||
String? _appSupportDirectory = 'Unknown';
|
||||
String? _documentsDirectory = 'Unknown';
|
||||
String? _cacheDirectory = 'Unknown';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@ -37,6 +38,7 @@ class _MyAppState extends State<MyApp> {
|
||||
String? downloadsDirectory;
|
||||
String? appSupportDirectory;
|
||||
String? documentsDirectory;
|
||||
String? cacheDirectory;
|
||||
final PathProviderWindows provider = PathProviderWindows();
|
||||
|
||||
try {
|
||||
@ -62,11 +64,18 @@ class _MyAppState extends State<MyApp> {
|
||||
appSupportDirectory = 'Failed to get app support directory: $exception';
|
||||
}
|
||||
|
||||
try {
|
||||
cacheDirectory = await provider.getApplicationCachePath();
|
||||
} catch (exception) {
|
||||
cacheDirectory = 'Failed to get cache directory: $exception';
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_tempDirectory = tempDirectory;
|
||||
_downloadsDirectory = downloadsDirectory;
|
||||
_appSupportDirectory = appSupportDirectory;
|
||||
_documentsDirectory = documentsDirectory;
|
||||
_cacheDirectory = cacheDirectory;
|
||||
});
|
||||
}
|
||||
|
||||
@ -84,6 +93,7 @@ class _MyAppState extends State<MyApp> {
|
||||
Text('Documents Directory: $_documentsDirectory\n'),
|
||||
Text('Downloads Directory: $_downloadsDirectory\n'),
|
||||
Text('Application Support Directory: $_appSupportDirectory\n'),
|
||||
Text('Cache Directory: $_cacheDirectory\n'),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
@ -114,30 +114,17 @@ class PathProviderWindows extends PathProviderPlatform {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getApplicationSupportPath() async {
|
||||
final String? appDataRoot =
|
||||
await getPath(WindowsKnownFolder.RoamingAppData);
|
||||
if (appDataRoot == null) {
|
||||
return null;
|
||||
}
|
||||
final Directory directory = Directory(
|
||||
path.join(appDataRoot, _getApplicationSpecificSubdirectory()));
|
||||
// Ensure that the directory exists if possible, since it will on other
|
||||
// platforms. If the name is longer than MAXPATH, creating will fail, so
|
||||
// skip that step; it's up to the client to decide what to do with the path
|
||||
// in that case (e.g., using a short path).
|
||||
if (directory.path.length <= MAX_PATH) {
|
||||
if (!directory.existsSync()) {
|
||||
await directory.create(recursive: true);
|
||||
}
|
||||
}
|
||||
return directory.path;
|
||||
}
|
||||
Future<String?> getApplicationSupportPath() =>
|
||||
_createApplicationSubdirectory(WindowsKnownFolder.RoamingAppData);
|
||||
|
||||
@override
|
||||
Future<String?> getApplicationDocumentsPath() =>
|
||||
getPath(WindowsKnownFolder.Documents);
|
||||
|
||||
@override
|
||||
Future<String?> getApplicationCachePath() =>
|
||||
_createApplicationSubdirectory(WindowsKnownFolder.LocalAppData);
|
||||
|
||||
@override
|
||||
Future<String?> getDownloadsPath() => getPath(WindowsKnownFolder.Downloads);
|
||||
|
||||
@ -256,4 +243,23 @@ class PathProviderWindows extends PathProviderPlatform {
|
||||
}
|
||||
return sanitized.isEmpty ? null : sanitized;
|
||||
}
|
||||
|
||||
Future<String?> _createApplicationSubdirectory(String folderId) async {
|
||||
final String? baseDir = await getPath(folderId);
|
||||
if (baseDir == null) {
|
||||
return null;
|
||||
}
|
||||
final Directory directory =
|
||||
Directory(path.join(baseDir, _getApplicationSpecificSubdirectory()));
|
||||
// Ensure that the directory exists if possible, since it will on other
|
||||
// platforms. If the name is longer than MAXPATH, creating will fail, so
|
||||
// skip that step; it's up to the client to decide what to do with the path
|
||||
// in that case (e.g., using a short path).
|
||||
if (directory.path.length <= MAX_PATH) {
|
||||
if (!directory.existsSync()) {
|
||||
await directory.create(recursive: true);
|
||||
}
|
||||
}
|
||||
return directory.path;
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ name: path_provider_windows
|
||||
description: Windows implementation of the path_provider plugin
|
||||
repository: https://github.com/flutter/packages/tree/main/packages/path_provider/path_provider_windows
|
||||
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
|
||||
version: 2.1.7
|
||||
version: 2.2.0
|
||||
|
||||
environment:
|
||||
sdk: ">=2.18.0 <4.0.0"
|
||||
@ -20,7 +20,7 @@ dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
path: ^1.8.0
|
||||
path_provider_platform_interface: ^2.0.0
|
||||
path_provider_platform_interface: ^2.1.0
|
||||
win32: ">=2.1.0 <6.0.0"
|
||||
|
||||
dev_dependencies:
|
||||
|
@ -165,6 +165,20 @@ void main() {
|
||||
expect(path, contains(r'Documents'));
|
||||
}, skip: !Platform.isWindows);
|
||||
|
||||
test('getApplicationCachePath', () async {
|
||||
final PathProviderWindows pathProvider = PathProviderWindows();
|
||||
pathProvider.versionInfoQuerier = FakeVersionInfoQuerier(<String, String>{
|
||||
'CompanyName': 'A Company',
|
||||
'ProductName': 'Amazing App',
|
||||
}, encoding: encodingCP1252);
|
||||
final String? path = await pathProvider.getApplicationCachePath();
|
||||
expect(path, isNotNull);
|
||||
if (path != null) {
|
||||
expect(path, endsWith(r'AppData\Local\A Company\Amazing App'));
|
||||
expect(Directory(path).existsSync(), isTrue);
|
||||
}
|
||||
}, skip: !Platform.isWindows);
|
||||
|
||||
test('getDownloadsPath', () async {
|
||||
final PathProviderWindows pathProvider = PathProviderWindows();
|
||||
final String? path = await pathProvider.getDownloadsPath();
|
||||
|
Reference in New Issue
Block a user