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 all 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 @@ -27,42 +27,51 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform {
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'));
Future<File?> _getLocalDataFile() async {
if (_localDataFilePath != null) {
return _localDataFilePath!;
}
return _localDataFilePath;
final directory = await pathProvider.getApplicationSupportPath();
if (directory == null) {
return null;
}
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!;
}
Map<String, Object> preferences = {};
final File? localDataFile = await _getLocalDataFile();
if (localDataFile != null && localDataFile.existsSync()) {
String stringMap = localDataFile.readAsStringSync();
if (stringMap.isNotEmpty) {
preferences = json.decode(stringMap).cast<String, Object>();
}
}
return _cachedPreferences;
_cachedPreferences = preferences;
return preferences;
}

/// 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();
final File? localDataFile = await _getLocalDataFile();
if (localDataFile == null) {
print("Unable to determine where to write preferences.");
return false;
}
if (!localDataFile.existsSync()) {
localDataFile.createSync(recursive: true);
}
Expand Down
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,20 @@ 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"
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.2

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;
late MemoryFileSystem fileSystem;
late PathProviderWindows pathProvider;

setUp(() {
fileSystem = MemoryFileSystem.test();
Expand All @@ -22,7 +22,7 @@ void main() {

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

_writeTestFile(String value) async {
Expand Down Expand Up @@ -87,23 +87,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';
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 => '';
}