Skip to content

Commit 0cec317

Browse files
authored
[shared_preferences_windows]-Migrate to null safety (flutter#3516)
Migrate shared_preferences_windows to null safety
1 parent aa827e3 commit 0cec317

File tree

4 files changed

+50
-35
lines changed

4 files changed

+50
-35
lines changed

packages/shared_preferences/shared_preferences_windows/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
## 0.0.3-nullsafety
3+
4+
* Migrate to null-safety.
5+
16
## 0.0.2+3
27

38
* Remove 'ffi' dependency.

packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,51 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform {
2727
PathProviderWindows pathProvider = PathProviderWindows();
2828

2929
/// Local copy of preferences
30-
Map<String, Object> _cachedPreferences;
30+
Map<String, Object>? _cachedPreferences;
3131

3232
/// Cached file for storing preferences.
33-
File _localDataFilePath;
33+
File? _localDataFilePath;
3434

3535
/// Gets the file where the preferences are stored.
36-
Future<File> _getLocalDataFile() async {
37-
if (_localDataFilePath == null) {
38-
final directory = await pathProvider.getApplicationSupportPath();
39-
_localDataFilePath =
40-
fs.file(path.join(directory, 'shared_preferences.json'));
36+
Future<File?> _getLocalDataFile() async {
37+
if (_localDataFilePath != null) {
38+
return _localDataFilePath!;
4139
}
42-
return _localDataFilePath;
40+
final directory = await pathProvider.getApplicationSupportPath();
41+
if (directory == null) {
42+
return null;
43+
}
44+
return _localDataFilePath =
45+
fs.file(path.join(directory, 'shared_preferences.json'));
4346
}
4447

4548
/// Gets the preferences from the stored file. Once read, the preferences are
4649
/// maintained in memory.
4750
Future<Map<String, Object>> _readPreferences() async {
48-
if (_cachedPreferences == null) {
49-
_cachedPreferences = {};
50-
File localDataFile = await _getLocalDataFile();
51-
if (localDataFile.existsSync()) {
52-
String stringMap = localDataFile.readAsStringSync();
53-
if (stringMap.isNotEmpty) {
54-
_cachedPreferences = json.decode(stringMap) as Map<String, Object>;
55-
}
51+
if (_cachedPreferences != null) {
52+
return _cachedPreferences!;
53+
}
54+
Map<String, Object> preferences = {};
55+
final File? localDataFile = await _getLocalDataFile();
56+
if (localDataFile != null && localDataFile.existsSync()) {
57+
String stringMap = localDataFile.readAsStringSync();
58+
if (stringMap.isNotEmpty) {
59+
preferences = json.decode(stringMap).cast<String, Object>();
5660
}
5761
}
58-
return _cachedPreferences;
62+
_cachedPreferences = preferences;
63+
return preferences;
5964
}
6065

6166
/// Writes the cached preferences to disk. Returns [true] if the operation
6267
/// succeeded.
6368
Future<bool> _writePreferences(Map<String, Object> preferences) async {
6469
try {
65-
File localDataFile = await _getLocalDataFile();
70+
final File? localDataFile = await _getLocalDataFile();
71+
if (localDataFile == null) {
72+
print("Unable to determine where to write preferences.");
73+
return false;
74+
}
6675
if (!localDataFile.existsSync()) {
6776
localDataFile.createSync(recursive: true);
6877
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
name: shared_preferences_windows
22
description: Windows implementation of shared_preferences
33
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences_windows
4-
version: 0.0.2+3
4+
version: 0.0.3-nullsafety
5+
56

67
flutter:
78
plugin:
@@ -11,20 +12,20 @@ flutter:
1112
pluginClass: none
1213

1314
environment:
14-
sdk: ">=2.1.0 <3.0.0"
15+
sdk: '>=2.12.0-0 <3.0.0'
1516
flutter: ">=1.12.8"
1617

1718
dependencies:
18-
shared_preferences_platform_interface: ^1.0.0
19+
shared_preferences_platform_interface: ^2.0.0-nullsafety
1920
flutter:
2021
sdk: flutter
21-
file: ">=5.1.0 <7.0.0"
22+
file: ^6.0.0-nullsafety.4
2223
meta: ^1.1.7
2324
path: ^1.6.4
24-
path_provider_platform_interface: ^1.0.3
25-
path_provider_windows: ^0.0.2
25+
path_provider_platform_interface: ^2.0.0-nullsafety
26+
path_provider_windows: ^0.1.0-nullsafety.2
2627

2728
dev_dependencies:
2829
flutter_test:
2930
sdk: flutter
30-
pedantic: ^1.8.0
31+
pedantic: ^1.10.0-nullsafety.3

packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import 'package:path_provider_windows/path_provider_windows.dart';
1010
import 'package:shared_preferences_windows/shared_preferences_windows.dart';
1111

1212
void main() {
13-
MemoryFileSystem fileSystem;
14-
PathProviderWindows pathProvider;
13+
late MemoryFileSystem fileSystem;
14+
late PathProviderWindows pathProvider;
1515

1616
setUp(() {
1717
fileSystem = MemoryFileSystem.test();
@@ -22,7 +22,7 @@ void main() {
2222

2323
Future<String> _getFilePath() async {
2424
final directory = await pathProvider.getApplicationSupportPath();
25-
return path.join(directory, 'shared_preferences.json');
25+
return path.join(directory!, 'shared_preferences.json');
2626
}
2727

2828
_writeTestFile(String value) async {
@@ -87,23 +87,23 @@ void main() {
8787
/// path it returns is a root path that does not actually exist on Windows.
8888
class FakePathProviderWindows extends PathProviderPlatform
8989
implements PathProviderWindows {
90-
VersionInfoQuerier versionInfoQuerier;
90+
late VersionInfoQuerier versionInfoQuerier;
9191

9292
@override
93-
Future<String> getApplicationSupportPath() async => r'C:\appsupport';
93+
Future<String?> getApplicationSupportPath() async => r'C:\appsupport';
9494

9595
@override
96-
Future<String> getTemporaryPath() async => null;
96+
Future<String?> getTemporaryPath() async => null;
9797

9898
@override
99-
Future<String> getLibraryPath() async => null;
99+
Future<String?> getLibraryPath() async => null;
100100

101101
@override
102-
Future<String> getApplicationDocumentsPath() async => null;
102+
Future<String?> getApplicationDocumentsPath() async => null;
103103

104104
@override
105-
Future<String> getDownloadsPath() async => null;
105+
Future<String?> getDownloadsPath() async => null;
106106

107107
@override
108-
Future<String> getPath(String folderID) async => null;
108+
Future<String> getPath(String folderID) async => '';
109109
}

0 commit comments

Comments
 (0)