Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Prev Previous commit
Next Next commit
Remove manual registration
  • Loading branch information
Emmanuel Garcia committed Feb 23, 2021
commit 6e6d3abcf08c331d289fc646c8da97e090e33007
38 changes: 8 additions & 30 deletions packages/path_provider/path_provider/lib/path_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export 'package:path_provider_platform_interface/path_provider_platform_interfac
@Deprecated('This is no longer necessary, and is now a no-op')
set disablePathProviderPlatformOverride(bool override) {}

bool _manualDartRegistrationNeeded = true;

/// An exception thrown when a directory that should always be available on
/// the current platform cannot be obtained.
class MissingPlatformDirectoryException implements Exception {
Expand All @@ -41,26 +39,6 @@ class MissingPlatformDirectoryException implements Exception {
}
}

PathProviderPlatform get _platform {
// This is to manually endorse Dart implementations until automatic
// registration of Dart plugins is implemented. For details see
// https://github.com/flutter/flutter/issues/52267.
if (_manualDartRegistrationNeeded) {
// Only do the initial registration if it hasn't already been overridden
// with a non-default instance.
if (!kIsWeb && PathProviderPlatform.instance is MethodChannelPathProvider) {
if (Platform.isLinux) {
PathProviderPlatform.instance = PathProviderLinux();
} else if (Platform.isWindows) {
PathProviderPlatform.instance = PathProviderWindows();
}
}
_manualDartRegistrationNeeded = false;
}

return PathProviderPlatform.instance;
}

/// Path to the temporary directory on the device that is not backed up and is
/// suitable for storing caches of downloaded files.
///
Expand All @@ -76,7 +54,7 @@ PathProviderPlatform get _platform {
/// Throws a `MissingPlatformDirectoryException` if the system is unable to
/// provide the directory.
Future<Directory> getTemporaryDirectory() async {
final String? path = await _platform.getTemporaryPath();
final String? path = await PathProviderPlatform.instance.getTemporaryPath();
if (path == null) {
throw MissingPlatformDirectoryException(
'Unable to get temporary directory');
Expand All @@ -98,7 +76,7 @@ Future<Directory> getTemporaryDirectory() async {
/// Throws a `MissingPlatformDirectoryException` if the system is unable to
/// provide the directory.
Future<Directory> getApplicationSupportDirectory() async {
final String? path = await _platform.getApplicationSupportPath();
final String? path = await PathProviderPlatform.instance.getApplicationSupportPath();
if (path == null) {
throw MissingPlatformDirectoryException(
'Unable to get application support directory');
Expand All @@ -116,7 +94,7 @@ Future<Directory> getApplicationSupportDirectory() async {
/// Throws a `MissingPlatformDirectoryException` if the system is unable to
/// provide the directory on a supported platform.
Future<Directory> getLibraryDirectory() async {
final String? path = await _platform.getLibraryPath();
final String? path = await PathProviderPlatform.instance.getLibraryPath();
if (path == null) {
throw MissingPlatformDirectoryException('Unable to get library directory');
}
Expand All @@ -136,7 +114,7 @@ Future<Directory> getLibraryDirectory() async {
/// Throws a `MissingPlatformDirectoryException` if the system is unable to
/// provide the directory.
Future<Directory> getApplicationDocumentsDirectory() async {
final String? path = await _platform.getApplicationDocumentsPath();
final String? path = await PathProviderPlatform.instance.getApplicationDocumentsPath();
if (path == null) {
throw MissingPlatformDirectoryException(
'Unable to get application documents directory');
Expand All @@ -153,7 +131,7 @@ Future<Directory> getApplicationDocumentsDirectory() async {
///
/// On Android this uses the `getExternalFilesDir(null)`.
Future<Directory?> getExternalStorageDirectory() async {
final String? path = await _platform.getExternalStoragePath();
final String? path = await PathProviderPlatform.instance.getExternalStoragePath();
if (path == null) {
return null;
}
Expand All @@ -174,7 +152,7 @@ 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();
final List<String>? paths = await PathProviderPlatform.instance.getExternalCachePaths();
if (paths == null) {
return null;
}
Expand All @@ -200,7 +178,7 @@ Future<List<Directory>?> getExternalStorageDirectories({
StorageDirectory? type,
}) async {
final List<String>? paths =
await _platform.getExternalStoragePaths(type: type);
await PathProviderPlatform.instance.getExternalStoragePaths(type: type);
if (paths == null) {
return null;
}
Expand All @@ -214,7 +192,7 @@ 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();
final String? path = await PathProviderPlatform.instance.getDownloadsPath();
if (path == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,6 @@ class SharedPreferences {
static Completer<SharedPreferences>? _completer;
static bool _manualDartRegistrationNeeded = true;

static SharedPreferencesStorePlatform get _store {
// This is to manually endorse the Linux implementation until automatic
// registration of dart plugins is implemented. For details see
// https://github.com/flutter/flutter/issues/52267.
if (_manualDartRegistrationNeeded) {
// Only do the initial registration if it hasn't already been overridden
// with a non-default instance.
if (!kIsWeb &&
SharedPreferencesStorePlatform.instance
is MethodChannelSharedPreferencesStore) {
if (Platform.isLinux) {
SharedPreferencesStorePlatform.instance = SharedPreferencesLinux();
} else if (Platform.isWindows) {
SharedPreferencesStorePlatform.instance = SharedPreferencesWindows();
}
}
_manualDartRegistrationNeeded = false;
}

return SharedPreferencesStorePlatform.instance;
}

/// Loads and parses the [SharedPreferences] for this app from disk.
///
/// Because this is reading from disk, it shouldn't be awaited in
Expand Down Expand Up @@ -140,7 +118,7 @@ class SharedPreferences {
Future<bool> remove(String key) {
final String prefixedKey = '$_prefix$key';
_preferenceCache.remove(key);
return _store.remove(prefixedKey);
return SharedPreferencesStorePlatform.instance.remove(prefixedKey);
}

Future<bool> _setValue(String valueType, String key, Object value) {
Expand All @@ -152,7 +130,7 @@ class SharedPreferences {
} else {
_preferenceCache[key] = value;
}
return _store.setValue(valueType, prefixedKey, value);
return SharedPreferencesStorePlatform.instance.setValue(valueType, prefixedKey, value);
}

/// Always returns true.
Expand All @@ -163,7 +141,7 @@ class SharedPreferences {
/// Completes with true once the user preferences for the app has been cleared.
Future<bool> clear() {
_preferenceCache.clear();
return _store.clear();
return SharedPreferencesStorePlatform.instance.clear();
}

/// Fetches the latest values from the host platform.
Expand All @@ -178,7 +156,8 @@ class SharedPreferences {
}

static Future<Map<String, Object>> _getSharedPreferencesMap() async {
final Map<String, Object> fromSystem = await _store.getAll();
final Map<String, Object> fromSystem =
await SharedPreferencesStorePlatform.instance.getAll();
assert(fromSystem != null);
// Strip the flutter. prefix from the returned preferences.
final Map<String, Object> preferencesMap = <String, Object>{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

#include "generated_plugin_registrant.h"

void RegisterPlugins(flutter::PluginRegistry* registry) {}

void RegisterPlugins(flutter::PluginRegistry* registry) {
}