Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 5 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,35 +27,37 @@ 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'));
if (_localDataFilePath != null) {
return _localDataFilePath!;
}
return _localDataFilePath;
final directory = await pathProvider.getApplicationSupportPath();
return _localDataFilePath =
fs.file(path.join(directory!, 'shared_preferences.json'));
Copy link
Contributor

Choose a reason for hiding this comment

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

Missed this. This isn't safe; directory is nullable because getApplicationSupportPath returns a nullable value. This needs to be checked, and a null return value propagated and handled.

(For read we can return a empty dictionary as with a missing file; for write I think we'll need to throw an error, as we don't know where we can write to in that case.)

Copy link
Contributor Author

@sameeerkashyap sameeerkashyap Feb 6, 2021

Choose a reason for hiding this comment

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

could you give me a bit more pointers on how the code should be? wrap it in a try and catch?
Or just and if condition and throw? Also What should I throw?

}

/// 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 = {};
File localDataFile = await _getLocalDataFile();
if (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
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,21 @@ flutter:
pluginClass: none

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: '>=2.12.0-259.8.beta <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 All @@ -46,7 +46,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 +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 => '';
}
2 changes: 1 addition & 1 deletion script/nnbd_plugins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ readonly NON_NNBD_PLUGINS_LIST=(
)

export EXCLUDED_PLUGINS_FROM_STABLE=$(IFS=, ; echo "${NNBD_PLUGINS_LIST[*]}")
export EXCLUDED_PLUGINS_FROM_MASTER=$(IFS=, ; echo "${NON_NNBD_PLUGINS_LIST[*]}")
export EXCLUDED_PLUGINS_FROM_MASTER=$(IFS=, ; echo "${NON_NNBD_PLUGINS_LIST[*]}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Please revert this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The new line?