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

[shared_preferences_windows]-Migrate to null safety #3516

Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

## 0.0.3-nullsafety

* Migrate to null-safety.

## 0.0.2+3

* Remove 'ffi' dependency.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,51 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform {

/// File system used to store to disk. Exposed for testing only.
@visibleForTesting
FileSystem fs = LocalFileSystem();
FileSystem? fs = LocalFileSystem();

/// The path_provider_windows instance used to find the support directory.
@visibleForTesting
PathProviderWindows pathProvider = PathProviderWindows();
PathProviderWindows? pathProvider = PathProviderWindows();

/// Local copy of preferences
Map<String, Object> _cachedPreferences;
Map<String, Object>? _cachedPreferences;

/// Cached file for storing preferences.
File _localDataFilePath;
File? _localDataFilePath;

/// Gets the file where the preferences are stored.
Future<File> _getLocalDataFile() async {
if (_localDataFilePath == null) {
final directory = await pathProvider.getApplicationSupportPath();
_localDataFilePath =
fs.file(path.join(directory, 'shared_preferences.json'));
if (_localDataFilePath != null) {
return _localDataFilePath!;
}
return _localDataFilePath;
final directory =
await (pathProvider!.getApplicationSupportPath() as FutureOr<String>);
return _localDataFilePath =
fs!.file(path.join(directory, 'shared_preferences.json'));
}

/// Gets the preferences from the stored file. Once read, the preferences are
/// maintained in memory.
Future<Map<String, Object>> _readPreferences() async {
if (_cachedPreferences == null) {
_cachedPreferences = {};
File localDataFile = await _getLocalDataFile();
if (localDataFile.existsSync()) {
String stringMap = localDataFile.readAsStringSync();
if (stringMap.isNotEmpty) {
_cachedPreferences = json.decode(stringMap) as Map<String, Object>;
}
if (_cachedPreferences != null) {
return (_cachedPreferences as Future<Map<String, Object>>);
}
Map<String, Object> _cachedPrefs = {};
File localDataFile = await (_getLocalDataFile());
if (localDataFile.existsSync()) {
String stringMap = localDataFile.readAsStringSync();
if (stringMap.isNotEmpty) {
_cachedPrefs = json.decode(stringMap).cast<String, Object>();
}
}
return _cachedPreferences;
return _cachedPrefs;
}

/// Writes the cached preferences to disk. Returns [true] if the operation
/// succeeded.
Future<bool> _writePreferences(Map<String, Object> preferences) async {
try {
File localDataFile = await _getLocalDataFile();
File localDataFile = await (_getLocalDataFile());
if (!localDataFile.existsSync()) {
localDataFile.createSync(recursive: true);
}
Expand All @@ -77,7 +79,7 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform {

@override
Future<bool> clear() async {
var preferences = await _readPreferences();
var preferences = await (_readPreferences());
preferences.clear();
return _writePreferences(preferences);
}
Expand All @@ -89,15 +91,16 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform {

@override
Future<bool> remove(String key) async {
var preferences = await _readPreferences();
var preferences = await (_readPreferences());
preferences.remove(key);
return _writePreferences(preferences);
}

@override
Future<bool> setValue(String valueType, String key, Object value) async {
var preferences = await _readPreferences();
var preferences = await (_readPreferences());
preferences[key] = value;
return _writePreferences(preferences);
}
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: shared_preferences_windows
description: Windows implementation of shared_preferences
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences_windows
version: 0.0.2+3
version: 0.0.3-nullsafety


flutter:
plugin:
Expand All @@ -11,20 +12,22 @@ flutter:
pluginClass: none

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: '>=2.12.0-0 <3.0.0'
flutter: ">=1.12.8"

dependencies:
shared_preferences_platform_interface: ^1.0.0
shared_preferences_platform_interface: ^2.0.0-nullsafety
flutter:
sdk: flutter
file: ">=5.1.0 <7.0.0"

ffi: ^0.2.0-nullsafety.1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was removed intentionally, and shouldn't be re-added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understand which one are you referring to?

file: ^6.0.0-nullsafety.4
meta: ^1.1.7
path: ^1.6.4
path_provider_platform_interface: ^1.0.3
path_provider_windows: ^0.0.2
path_provider_platform_interface: ^2.0.0-nullsafety
path_provider_windows: ^0.1.0-nullsafety.1

dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.8.0
pedantic: ^1.10.0-nullsafety.3
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import 'package:path_provider_windows/path_provider_windows.dart';
import 'package:shared_preferences_windows/shared_preferences_windows.dart';

void main() {
MemoryFileSystem fileSystem;
PathProviderWindows pathProvider;
MemoryFileSystem? fileSystem;
PathProviderWindows? pathProvider;

setUp(() {
fileSystem = MemoryFileSystem.test();
Expand All @@ -21,18 +21,19 @@ void main() {
tearDown(() {});

Future<String> _getFilePath() async {
final directory = await pathProvider.getApplicationSupportPath();
final directory =
await (pathProvider!.getApplicationSupportPath() as Future<String>);
return path.join(directory, 'shared_preferences.json');
}

_writeTestFile(String value) async {
fileSystem.file(await _getFilePath())
fileSystem!.file(await _getFilePath())
..createSync(recursive: true)
..writeAsStringSync(value);
}

Future<String> _readTestFile() async {
return fileSystem.file(await _getFilePath()).readAsStringSync();
return fileSystem!.file(await _getFilePath()).readAsStringSync();
}

SharedPreferencesWindows _getPreferences() {
Expand All @@ -46,7 +47,7 @@ void main() {
await _writeTestFile('{"key1": "one", "key2": 2}');
var prefs = _getPreferences();

var values = await prefs.getAll();
var values = await (prefs.getAll());
expect(values, hasLength(2));
expect(values['key1'], 'one');
expect(values['key2'], 2);
Expand Down Expand Up @@ -87,23 +88,23 @@ void main() {
/// path it returns is a root path that does not actually exist on Windows.
class FakePathProviderWindows extends PathProviderPlatform
implements PathProviderWindows {
VersionInfoQuerier versionInfoQuerier;
late VersionInfoQuerier versionInfoQuerier;

@override
Future<String> getApplicationSupportPath() async => r'C:\appsupport';

@override
Future<String> getTemporaryPath() async => null;
Future<String?> getTemporaryPath() async => null;

@override
Future<String> getLibraryPath() async => null;
Future<String?> getLibraryPath() async => null;

@override
Future<String> getApplicationDocumentsPath() async => null;
Future<String?> getApplicationDocumentsPath() async => null;

@override
Future<String> getDownloadsPath() async => null;
Future<String?> getDownloadsPath() async => null;

@override
Future<String> getPath(String folderID) async => null;
Future<String> getPath(String folderID) async => '';
}
1 change: 1 addition & 0 deletions script/nnbd_plugins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ readonly NNBD_PLUGINS_LIST=(
"url_launcher"
"video_player"
"webview_flutter"
"shared_preferences_windows"
"image_picker"
)

Expand Down