From 2e22820df8f7b9f4cd5bd7eb2c3ccd19342501ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Sat, 6 Feb 2021 03:30:44 +0100 Subject: [PATCH 01/12] [shared_preferences] Migrate platform plugins to nnbd --- .../lib/shared_preferences_linux.dart | 12 ++++----- .../shared_preferences_linux/pubspec.yaml | 11 ++++---- .../test/shared_preferences_linux_test.dart | 8 +++--- .../lib/shared_preferences_web.dart | 20 +++++--------- .../shared_preferences_web/pubspec.yaml | 6 ++--- .../test/shared_preferences_web_test.dart | 8 ++---- .../lib/shared_preferences_windows.dart | 22 +++++++-------- .../shared_preferences_windows/pubspec.yaml | 12 ++++----- .../test/shared_preferences_windows_test.dart | 27 +++++++++---------- 9 files changed, 57 insertions(+), 69 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart index c975ad1a7544..114a5a6b399c 100644 --- a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart +++ b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart @@ -20,24 +20,24 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { static SharedPreferencesLinux instance = SharedPreferencesLinux(); /// Local copy of preferences - Map _cachedPreferences; + Map? _cachedPreferences; /// File system used to store to disk. Exposed for testing only. @visibleForTesting - FileSystem fs = LocalFileSystem(); + FileSystem? fs = LocalFileSystem(); /// Gets the file where the preferences are stored. Future _getLocalDataFile() async { final pathProvider = PathProviderLinux(); final directory = await pathProvider.getApplicationSupportPath(); - return fs.file(path.join(directory, 'shared_preferences.json')); + return fs!.file(path.join(directory!, 'shared_preferences.json')); } /// Gets the preferences from the stored file. Once read, the preferences are /// maintained in memory. Future> _readPreferences() async { if (_cachedPreferences != null) { - return _cachedPreferences; + return _cachedPreferences!; } _cachedPreferences = {}; @@ -45,11 +45,11 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { if (localDataFile.existsSync()) { String stringMap = localDataFile.readAsStringSync(); if (stringMap.isNotEmpty) { - _cachedPreferences = json.decode(stringMap) as Map; + _cachedPreferences = json.decode(stringMap) as Map?; } } - return _cachedPreferences; + return _cachedPreferences!; } /// Writes the cached preferences to disk. Returns [true] if the operation diff --git a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml index 50709aac5f8a..e7439b6ff053 100644 --- a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml @@ -1,6 +1,6 @@ name: shared_preferences_linux description: Linux implementation of the shared_preferences plugin -version: 0.0.3+1 +version: 0.0.4-nullsafety homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences_linux flutter: @@ -11,17 +11,16 @@ 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: - file: ">=5.1.0 <7.0.0" + file: ">=6.0.0-nullsafety.4 <7.0.0" flutter: sdk: flutter meta: ^1.0.4 - path: ^1.6.4 - path_provider_linux: ^0.0.1 - shared_preferences_platform_interface: ^1.0.0 + path_provider_linux: ^0.2.0-nullsafety + shared_preferences_platform_interface: ^2.0.0-nullsafety dev_dependencies: flutter_test: diff --git a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart index 8c659f212aa5..b5c93ac7fc3c 100644 --- a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart +++ b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart @@ -7,7 +7,7 @@ import 'package:path/path.dart' as path; import 'package:path_provider_linux/path_provider_linux.dart'; import 'package:shared_preferences_linux/shared_preferences_linux.dart'; -MemoryFileSystem fs; +MemoryFileSystem? fs; void main() { setUp(() { @@ -19,17 +19,17 @@ void main() { Future _getFilePath() async { final pathProvider = PathProviderLinux(); final directory = await pathProvider.getApplicationSupportPath(); - return path.join(directory, 'shared_preferences.json'); + return path.join(directory!, 'shared_preferences.json'); } _writeTestFile(String value) async { - fs.file(await _getFilePath()) + fs!.file(await _getFilePath()) ..createSync(recursive: true) ..writeAsStringSync(value); } Future _readTestFile() async { - return fs.file(await _getFilePath()).readAsStringSync(); + return fs!.file(await _getFilePath()).readAsStringSync(); } SharedPreferencesLinux _getPreferences() { diff --git a/packages/shared_preferences/shared_preferences_web/lib/shared_preferences_web.dart b/packages/shared_preferences/shared_preferences_web/lib/shared_preferences_web.dart index 8a0f137ddcc8..5419b2f890a0 100644 --- a/packages/shared_preferences/shared_preferences_web/lib/shared_preferences_web.dart +++ b/packages/shared_preferences/shared_preferences_web/lib/shared_preferences_web.dart @@ -14,7 +14,7 @@ import 'package:shared_preferences_platform_interface/shared_preferences_platfor /// This class implements the `package:shared_preferences` functionality for the web. class SharedPreferencesPlugin extends SharedPreferencesStorePlatform { /// Registers this class as the default instance of [SharedPreferencesStorePlatform]. - static void registerWith(Registrar registrar) { + static void registerWith(Registrar? registrar) { SharedPreferencesStorePlatform.instance = SharedPreferencesPlugin(); } @@ -31,9 +31,9 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform { @override Future> getAll() async { - final Map allData = {}; + final Map allData = {}; for (String key in _storedFlutterKeys) { - allData[key] = _decodeValue(html.window.localStorage[key]); + allData[key] = _decodeValue(html.window.localStorage[key]!); } return allData; } @@ -46,7 +46,7 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform { } @override - Future setValue(String valueType, String key, Object value) async { + Future setValue(String valueType, String key, Object? value) async { _checkPrefix(key); html.window.localStorage[key] = _encodeValue(value); return true; @@ -62,17 +62,11 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform { } } - List get _storedFlutterKeys { - final List keys = []; - for (String key in html.window.localStorage.keys) { - if (key.startsWith('flutter.')) { - keys.add(key); - } - } - return keys; + Iterable get _storedFlutterKeys { + return html.window.localStorage.keys.where((key) => key.startsWith('flutter.')); } - String _encodeValue(Object value) { + String _encodeValue(Object? value) { return json.encode(value); } diff --git a/packages/shared_preferences/shared_preferences_web/pubspec.yaml b/packages/shared_preferences/shared_preferences_web/pubspec.yaml index d657b2300727..eb124105d9b6 100644 --- a/packages/shared_preferences/shared_preferences_web/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_web/pubspec.yaml @@ -4,7 +4,7 @@ homepage: https://github.com/flutter/plugins/tree/master/packages/shared_prefere # 0.1.y+z is compatible with 1.0.0, if you land a breaking change bump # the version to 2.0.0. # See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0 -version: 0.1.2+8 +version: 0.2.0-nullsafety flutter: plugin: @@ -14,7 +14,7 @@ flutter: fileName: shared_preferences_web.dart dependencies: - shared_preferences_platform_interface: ^1.0.0 + shared_preferences_platform_interface: ^2.0.0-nullsafety flutter: sdk: flutter flutter_web_plugins: @@ -27,5 +27,5 @@ dev_dependencies: pedantic: ^1.8.0 environment: - sdk: ">=2.1.0 <3.0.0" + sdk: ">=2.12.0-259.8.beta <3.0.0" flutter: ">=1.12.13+hotfix.4" diff --git a/packages/shared_preferences/shared_preferences_web/test/shared_preferences_web_test.dart b/packages/shared_preferences/shared_preferences_web/test/shared_preferences_web_test.dart index 951f04cbce5a..1c77bb4d5be1 100644 --- a/packages/shared_preferences/shared_preferences_web/test/shared_preferences_web_test.dart +++ b/packages/shared_preferences/shared_preferences_web/test/shared_preferences_web_test.dart @@ -2,8 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -@TestOn('chrome') // Uses web-only Flutter SDK - +@TestOn('chrome') import 'dart:convert' show json; import 'dart:html' as html; @@ -26,11 +25,8 @@ void main() { }); test('registers itself', () { - expect(SharedPreferencesStorePlatform.instance, - isNot(isA())); SharedPreferencesPlugin.registerWith(null); - expect(SharedPreferencesStorePlatform.instance, - isA()); + expect(SharedPreferencesStorePlatform.instance, isA()); }); test('getAll', () async { diff --git a/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart b/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart index dd9ab8a0c38f..54522d296dd0 100644 --- a/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart +++ b/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart @@ -4,12 +4,13 @@ import 'dart:async'; import 'dart:convert' show json; + import 'package:file/file.dart'; import 'package:file/local.dart'; import 'package:meta/meta.dart'; import 'package:path/path.dart' as path; -import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; import 'package:path_provider_windows/path_provider_windows.dart'; +import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; /// The Windows implementation of [SharedPreferencesStorePlatform]. /// @@ -20,26 +21,25 @@ 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 _cachedPreferences; + Map? _cachedPreferences; /// Cached file for storing preferences. - File _localDataFilePath; + File? _localDataFilePath; /// Gets the file where the preferences are stored. Future _getLocalDataFile() async { if (_localDataFilePath == null) { - final directory = await pathProvider.getApplicationSupportPath(); - _localDataFilePath = - fs.file(path.join(directory, 'shared_preferences.json')); + final directory = await pathProvider!.getApplicationSupportPath(); + _localDataFilePath = fs!.file(path.join(directory!, 'shared_preferences.json')); } - return _localDataFilePath; + return _localDataFilePath!; } /// Gets the preferences from the stored file. Once read, the preferences are @@ -51,11 +51,11 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform { if (localDataFile.existsSync()) { String stringMap = localDataFile.readAsStringSync(); if (stringMap.isNotEmpty) { - _cachedPreferences = json.decode(stringMap) as Map; + _cachedPreferences = (json.decode(stringMap) as Map).cast(); } } } - return _cachedPreferences; + return _cachedPreferences!; } /// Writes the cached preferences to disk. Returns [true] if the operation diff --git a/packages/shared_preferences/shared_preferences_windows/pubspec.yaml b/packages/shared_preferences/shared_preferences_windows/pubspec.yaml index 6123300c9689..4fe24f48d665 100644 --- a/packages/shared_preferences/shared_preferences_windows/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_windows/pubspec.yaml @@ -1,7 +1,7 @@ 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: @@ -11,18 +11,18 @@ 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 <7.0.0" 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: diff --git a/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart b/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart index b0827ca3b36b..14a5c0d1e0bd 100644 --- a/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart +++ b/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart @@ -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(); @@ -21,18 +21,18 @@ void main() { tearDown(() {}); Future _getFilePath() async { - final directory = await pathProvider.getApplicationSupportPath(); - return path.join(directory, 'shared_preferences.json'); + final directory = await pathProvider!.getApplicationSupportPath(); + 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 _readTestFile() async { - return fileSystem.file(await _getFilePath()).readAsStringSync(); + return fileSystem!.file(await _getFilePath()).readAsStringSync(); } SharedPreferencesWindows _getPreferences() { @@ -85,25 +85,24 @@ void main() { /// /// Note that this should only be used with an in-memory filesystem, as the /// path it returns is a root path that does not actually exist on Windows. -class FakePathProviderWindows extends PathProviderPlatform - implements PathProviderWindows { - VersionInfoQuerier versionInfoQuerier; +class FakePathProviderWindows extends PathProviderPlatform implements PathProviderWindows { + late VersionInfoQuerier versionInfoQuerier; @override Future getApplicationSupportPath() async => r'C:\appsupport'; @override - Future getTemporaryPath() async => null; + Future getTemporaryPath() async => null; @override - Future getLibraryPath() async => null; + Future getLibraryPath() async => null; @override - Future getApplicationDocumentsPath() async => null; + Future getApplicationDocumentsPath() async => null; @override - Future getDownloadsPath() async => null; + Future getDownloadsPath() async => null; @override - Future getPath(String folderID) async => null; + Future getPath(String folderID) async => ''; } From 235cef016b136bd30f71f3ad25dadf7eef62abea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Sat, 6 Feb 2021 03:38:55 +0100 Subject: [PATCH 02/12] Update CHANGELOGs --- .../shared_preferences/shared_preferences_linux/CHANGELOG.md | 4 ++++ .../shared_preferences/shared_preferences_web/CHANGELOG.md | 4 ++++ .../shared_preferences_windows/CHANGELOG.md | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md index 9821b79c5cc2..2be9c8ca075a 100644 --- a/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.0.4-nullsafety + +* Migrate to null-safety. + ## 0.0.3+1 * Update Flutter SDK constraint. diff --git a/packages/shared_preferences/shared_preferences_web/CHANGELOG.md b/packages/shared_preferences/shared_preferences_web/CHANGELOG.md index 2ba877856da6..0194ef8ade37 100644 --- a/packages/shared_preferences/shared_preferences_web/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences_web/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.0-nullsafety + +* Migrate to null-safety. + ## 0.1.2+8 * Update Flutter SDK constraint. diff --git a/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md b/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md index a8c3a85cd3ce..07ad7b436a30 100644 --- a/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.0.3-nullsafety + +* Migrate to null-safety. + ## 0.0.2+3 * Remove 'ffi' dependency. From e19321f098ea433cc87f934720886f070c34e1fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Sat, 6 Feb 2021 03:49:51 +0100 Subject: [PATCH 03/12] fixed dependency issues --- .../shared_preferences_linux/example/pubspec.yaml | 4 +--- .../shared_preferences/shared_preferences_linux/pubspec.yaml | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml index fb79444dc6cd..8845e4ea7605 100644 --- a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml @@ -5,14 +5,12 @@ dependencies: flutter: sdk: flutter shared_preferences: any - shared_preferences_linux: ^0.1.0 dependency_overrides: shared_preferences_linux: path: ../ - # Remove this override once the endorsement is published. shared_preferences: - path: ../../shared_preferences/ + path: ../../shared_preferences dev_dependencies: flutter_driver: diff --git a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml index e7439b6ff053..df5d6c2d02e2 100644 --- a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml @@ -15,10 +15,11 @@ environment: flutter: ">=1.12.8" dependencies: - file: ">=6.0.0-nullsafety.4 <7.0.0" flutter: sdk: flutter + file: ">=6.0.0-nullsafety.4 <7.0.0" meta: ^1.0.4 + path: ^1.8.0-nullsafety.3 path_provider_linux: ^0.2.0-nullsafety shared_preferences_platform_interface: ^2.0.0-nullsafety From e2e33ec152e50c46761a323eb51efe1c0d5420b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Sat, 6 Feb 2021 03:52:53 +0100 Subject: [PATCH 04/12] fix fortmatting --- .../shared_preferences_web/lib/shared_preferences_web.dart | 3 ++- .../test/shared_preferences_web_test.dart | 3 ++- .../lib/shared_preferences_windows.dart | 6 ++++-- .../test/shared_preferences_windows_test.dart | 3 ++- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_web/lib/shared_preferences_web.dart b/packages/shared_preferences/shared_preferences_web/lib/shared_preferences_web.dart index 5419b2f890a0..346877e8a120 100644 --- a/packages/shared_preferences/shared_preferences_web/lib/shared_preferences_web.dart +++ b/packages/shared_preferences/shared_preferences_web/lib/shared_preferences_web.dart @@ -63,7 +63,8 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform { } Iterable get _storedFlutterKeys { - return html.window.localStorage.keys.where((key) => key.startsWith('flutter.')); + return html.window.localStorage.keys + .where((key) => key.startsWith('flutter.')); } String _encodeValue(Object? value) { diff --git a/packages/shared_preferences/shared_preferences_web/test/shared_preferences_web_test.dart b/packages/shared_preferences/shared_preferences_web/test/shared_preferences_web_test.dart index 1c77bb4d5be1..9dab5665da22 100644 --- a/packages/shared_preferences/shared_preferences_web/test/shared_preferences_web_test.dart +++ b/packages/shared_preferences/shared_preferences_web/test/shared_preferences_web_test.dart @@ -26,7 +26,8 @@ void main() { test('registers itself', () { SharedPreferencesPlugin.registerWith(null); - expect(SharedPreferencesStorePlatform.instance, isA()); + expect(SharedPreferencesStorePlatform.instance, + isA()); }); test('getAll', () async { diff --git a/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart b/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart index 54522d296dd0..53868de7b1d7 100644 --- a/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart +++ b/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart @@ -37,7 +37,8 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform { Future _getLocalDataFile() async { if (_localDataFilePath == null) { final directory = await pathProvider!.getApplicationSupportPath(); - _localDataFilePath = fs!.file(path.join(directory!, 'shared_preferences.json')); + _localDataFilePath = + fs!.file(path.join(directory!, 'shared_preferences.json')); } return _localDataFilePath!; } @@ -51,7 +52,8 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform { if (localDataFile.existsSync()) { String stringMap = localDataFile.readAsStringSync(); if (stringMap.isNotEmpty) { - _cachedPreferences = (json.decode(stringMap) as Map).cast(); + _cachedPreferences = + (json.decode(stringMap) as Map).cast(); } } } diff --git a/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart b/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart index 14a5c0d1e0bd..a0306ec06592 100644 --- a/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart +++ b/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart @@ -85,7 +85,8 @@ void main() { /// /// Note that this should only be used with an in-memory filesystem, as the /// path it returns is a root path that does not actually exist on Windows. -class FakePathProviderWindows extends PathProviderPlatform implements PathProviderWindows { +class FakePathProviderWindows extends PathProviderPlatform + implements PathProviderWindows { late VersionInfoQuerier versionInfoQuerier; @override From 111922610c26b7686fa0df0eaaecfcf8374a5306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Sat, 6 Feb 2021 04:12:01 +0100 Subject: [PATCH 05/12] fix linux test --- .../shared_preferences_linux/lib/shared_preferences_linux.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart index 114a5a6b399c..2e0a44b130aa 100644 --- a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart +++ b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart @@ -45,7 +45,8 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { if (localDataFile.existsSync()) { String stringMap = localDataFile.readAsStringSync(); if (stringMap.isNotEmpty) { - _cachedPreferences = json.decode(stringMap) as Map?; + _cachedPreferences = + (json.decode(stringMap) as Map).cast(); } } From dad45b806826e6715b7b4470a7bb1c66416c7b36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Sat, 6 Feb 2021 04:40:12 +0100 Subject: [PATCH 06/12] fix windows example dependencies --- .../shared_preferences_windows/example/pubspec.yaml | 3 --- .../example/windows/flutter/CMakeLists.txt | 1 + .../example/windows/flutter/generated_plugin_registrant.cc | 4 +++- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_windows/example/pubspec.yaml b/packages/shared_preferences/shared_preferences_windows/example/pubspec.yaml index 8a44e7874cfb..7ef6a228c426 100644 --- a/packages/shared_preferences/shared_preferences_windows/example/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_windows/example/pubspec.yaml @@ -7,9 +7,6 @@ environment: dependencies: flutter: sdk: flutter - shared_preferences_windows: ^0.0.1 - -dependency_overrides: shared_preferences_windows: path: ../ diff --git a/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/CMakeLists.txt b/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/CMakeLists.txt index c7a8c7607d81..744f08a9389b 100644 --- a/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/CMakeLists.txt +++ b/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/CMakeLists.txt @@ -91,6 +91,7 @@ add_custom_command( ${FLUTTER_TOOL_ENVIRONMENT} "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.bat" windows-x64 $ + VERBATIM ) add_custom_target(flutter_assemble DEPENDS "${FLUTTER_LIBRARY}" diff --git a/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/generated_plugin_registrant.cc b/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/generated_plugin_registrant.cc index a6177ab0b72b..4bfa0f3a3a75 100644 --- a/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/generated_plugin_registrant.cc +++ b/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/generated_plugin_registrant.cc @@ -4,4 +4,6 @@ #include "generated_plugin_registrant.h" -void RegisterPlugins(flutter::PluginRegistry* registry) {} + +void RegisterPlugins(flutter::PluginRegistry* registry) { +} From 798a9b6a9fe021b2f6ed992f6a50826c6c11664a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Sat, 6 Feb 2021 04:56:02 +0100 Subject: [PATCH 07/12] Fixed generated_plugin_registrant format --- .../example/windows/flutter/generated_plugin_registrant.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/generated_plugin_registrant.cc b/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/generated_plugin_registrant.cc index 4bfa0f3a3a75..a6177ab0b72b 100644 --- a/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/generated_plugin_registrant.cc +++ b/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/generated_plugin_registrant.cc @@ -4,6 +4,4 @@ #include "generated_plugin_registrant.h" - -void RegisterPlugins(flutter::PluginRegistry* registry) { -} +void RegisterPlugins(flutter::PluginRegistry* registry) {} From 6cf839c91d6866eed36fc43fc5ea86cbccb7a7b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Sat, 6 Feb 2021 06:55:56 +0100 Subject: [PATCH 08/12] Fix dependencies again + unnecessary nullable field --- .../shared_preferences_linux/example/pubspec.yaml | 4 ++-- .../lib/shared_preferences_linux.dart | 4 ++-- .../test/shared_preferences_linux_test.dart | 8 ++++---- .../lib/shared_preferences_windows.dart | 8 ++++---- .../test/shared_preferences_windows_test.dart | 12 ++++++------ 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml index 8845e4ea7605..3925205e2f2a 100644 --- a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml @@ -9,8 +9,8 @@ dependencies: dependency_overrides: shared_preferences_linux: path: ../ - shared_preferences: - path: ../../shared_preferences + shared_preferences_platform_interface: + path: ../../shared_preferences_platform_interface dev_dependencies: flutter_driver: diff --git a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart index 2e0a44b130aa..ac4a728d0d13 100644 --- a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart +++ b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart @@ -24,13 +24,13 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { /// File system used to store to disk. Exposed for testing only. @visibleForTesting - FileSystem? fs = LocalFileSystem(); + FileSystem fs = LocalFileSystem(); /// Gets the file where the preferences are stored. Future _getLocalDataFile() async { final pathProvider = PathProviderLinux(); final directory = await pathProvider.getApplicationSupportPath(); - return fs!.file(path.join(directory!, 'shared_preferences.json')); + return fs.file(path.join(directory!, 'shared_preferences.json')); } /// Gets the preferences from the stored file. Once read, the preferences are diff --git a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart index b5c93ac7fc3c..cf0bc80e3ec2 100644 --- a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart +++ b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart @@ -7,9 +7,9 @@ import 'package:path/path.dart' as path; import 'package:path_provider_linux/path_provider_linux.dart'; import 'package:shared_preferences_linux/shared_preferences_linux.dart'; -MemoryFileSystem? fs; - void main() { + late MemoryFileSystem fs; + setUp(() { fs = MemoryFileSystem.test(); }); @@ -23,13 +23,13 @@ void main() { } _writeTestFile(String value) async { - fs!.file(await _getFilePath()) + fs.file(await _getFilePath()) ..createSync(recursive: true) ..writeAsStringSync(value); } Future _readTestFile() async { - return fs!.file(await _getFilePath()).readAsStringSync(); + return fs.file(await _getFilePath()).readAsStringSync(); } SharedPreferencesLinux _getPreferences() { diff --git a/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart b/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart index 53868de7b1d7..001c1847af6e 100644 --- a/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart +++ b/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart @@ -21,11 +21,11 @@ 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? _cachedPreferences; @@ -36,9 +36,9 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform { /// Gets the file where the preferences are stored. Future _getLocalDataFile() async { if (_localDataFilePath == null) { - final directory = await pathProvider!.getApplicationSupportPath(); + final directory = await pathProvider.getApplicationSupportPath(); _localDataFilePath = - fs!.file(path.join(directory!, 'shared_preferences.json')); + fs.file(path.join(directory!, 'shared_preferences.json')); } return _localDataFilePath!; } diff --git a/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart b/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart index a0306ec06592..785092f6fa16 100644 --- a/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart +++ b/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart @@ -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(); @@ -21,18 +21,18 @@ void main() { tearDown(() {}); Future _getFilePath() async { - final directory = await pathProvider!.getApplicationSupportPath(); + final directory = await pathProvider.getApplicationSupportPath(); 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 _readTestFile() async { - return fileSystem!.file(await _getFilePath()).readAsStringSync(); + return fileSystem.file(await _getFilePath()).readAsStringSync(); } SharedPreferencesWindows _getPreferences() { @@ -90,7 +90,7 @@ class FakePathProviderWindows extends PathProviderPlatform late VersionInfoQuerier versionInfoQuerier; @override - Future getApplicationSupportPath() async => r'C:\appsupport'; + Future getApplicationSupportPath() async => r'C:\appsupport'; @override Future getTemporaryPath() async => null; From c4acbf3edbf3811ae97867694fa00b3fdb2cbecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Sat, 6 Feb 2021 19:44:59 +0100 Subject: [PATCH 09/12] Fix file version + linux logic for _cached --- .../shared_preferences_linux/lib/shared_preferences_linux.dart | 2 +- .../shared_preferences/shared_preferences_linux/pubspec.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart index ac4a728d0d13..16f61293fcf5 100644 --- a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart +++ b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart @@ -40,7 +40,6 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { return _cachedPreferences!; } - _cachedPreferences = {}; var localDataFile = await _getLocalDataFile(); if (localDataFile.existsSync()) { String stringMap = localDataFile.readAsStringSync(); @@ -50,6 +49,7 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { } } + _cachedPreferences ??= {}; return _cachedPreferences!; } diff --git a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml index df5d6c2d02e2..9a51976aafaf 100644 --- a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml @@ -17,7 +17,7 @@ environment: dependencies: flutter: sdk: flutter - file: ">=6.0.0-nullsafety.4 <7.0.0" + file: ^6.0.0-nullsafety.4 meta: ^1.0.4 path: ^1.8.0-nullsafety.3 path_provider_linux: ^0.2.0-nullsafety From 827e55f509f3d973aefafeb5e20f3cd66fae864b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Sat, 6 Feb 2021 19:47:09 +0100 Subject: [PATCH 10/12] Revert windows --- .../shared_preferences_windows/CHANGELOG.md | 4 ---- .../example/pubspec.yaml | 3 +++ .../example/windows/flutter/CMakeLists.txt | 1 - .../lib/shared_preferences_windows.dart | 16 +++++++-------- .../shared_preferences_windows/pubspec.yaml | 12 +++++------ .../test/shared_preferences_windows_test.dart | 20 +++++++++---------- 6 files changed, 26 insertions(+), 30 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md b/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md index 07ad7b436a30..a8c3a85cd3ce 100644 --- a/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md @@ -1,7 +1,3 @@ -## 0.0.3-nullsafety - -* Migrate to null-safety. - ## 0.0.2+3 * Remove 'ffi' dependency. diff --git a/packages/shared_preferences/shared_preferences_windows/example/pubspec.yaml b/packages/shared_preferences/shared_preferences_windows/example/pubspec.yaml index 7ef6a228c426..8a44e7874cfb 100644 --- a/packages/shared_preferences/shared_preferences_windows/example/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_windows/example/pubspec.yaml @@ -7,6 +7,9 @@ environment: dependencies: flutter: sdk: flutter + shared_preferences_windows: ^0.0.1 + +dependency_overrides: shared_preferences_windows: path: ../ diff --git a/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/CMakeLists.txt b/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/CMakeLists.txt index 744f08a9389b..c7a8c7607d81 100644 --- a/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/CMakeLists.txt +++ b/packages/shared_preferences/shared_preferences_windows/example/windows/flutter/CMakeLists.txt @@ -91,7 +91,6 @@ add_custom_command( ${FLUTTER_TOOL_ENVIRONMENT} "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.bat" windows-x64 $ - VERBATIM ) add_custom_target(flutter_assemble DEPENDS "${FLUTTER_LIBRARY}" diff --git a/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart b/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart index 001c1847af6e..dd9ab8a0c38f 100644 --- a/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart +++ b/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart @@ -4,13 +4,12 @@ import 'dart:async'; import 'dart:convert' show json; - import 'package:file/file.dart'; import 'package:file/local.dart'; import 'package:meta/meta.dart'; import 'package:path/path.dart' as path; -import 'package:path_provider_windows/path_provider_windows.dart'; import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; +import 'package:path_provider_windows/path_provider_windows.dart'; /// The Windows implementation of [SharedPreferencesStorePlatform]. /// @@ -28,19 +27,19 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform { PathProviderWindows pathProvider = PathProviderWindows(); /// Local copy of preferences - Map? _cachedPreferences; + Map _cachedPreferences; /// Cached file for storing preferences. - File? _localDataFilePath; + File _localDataFilePath; /// Gets the file where the preferences are stored. Future _getLocalDataFile() async { if (_localDataFilePath == null) { final directory = await pathProvider.getApplicationSupportPath(); _localDataFilePath = - fs.file(path.join(directory!, 'shared_preferences.json')); + fs.file(path.join(directory, 'shared_preferences.json')); } - return _localDataFilePath!; + return _localDataFilePath; } /// Gets the preferences from the stored file. Once read, the preferences are @@ -52,12 +51,11 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform { if (localDataFile.existsSync()) { String stringMap = localDataFile.readAsStringSync(); if (stringMap.isNotEmpty) { - _cachedPreferences = - (json.decode(stringMap) as Map).cast(); + _cachedPreferences = json.decode(stringMap) as Map; } } } - return _cachedPreferences!; + return _cachedPreferences; } /// Writes the cached preferences to disk. Returns [true] if the operation diff --git a/packages/shared_preferences/shared_preferences_windows/pubspec.yaml b/packages/shared_preferences/shared_preferences_windows/pubspec.yaml index 4fe24f48d665..6123300c9689 100644 --- a/packages/shared_preferences/shared_preferences_windows/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_windows/pubspec.yaml @@ -1,7 +1,7 @@ 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.3-nullsafety +version: 0.0.2+3 flutter: plugin: @@ -11,18 +11,18 @@ flutter: pluginClass: none environment: - sdk: ">=2.12.0-259.8.beta <3.0.0" + sdk: ">=2.1.0 <3.0.0" flutter: ">=1.12.8" dependencies: - shared_preferences_platform_interface: ^2.0.0-nullsafety + shared_preferences_platform_interface: ^1.0.0 flutter: sdk: flutter - file: ">=6.0.0-nullsafety.4 <7.0.0" + file: ">=5.1.0 <7.0.0" meta: ^1.1.7 path: ^1.6.4 - path_provider_platform_interface: ^2.0.0-nullsafety - path_provider_windows: ^0.1.0-nullsafety.2 + path_provider_platform_interface: ^1.0.3 + path_provider_windows: ^0.0.2 dev_dependencies: flutter_test: diff --git a/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart b/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart index 785092f6fa16..b0827ca3b36b 100644 --- a/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart +++ b/packages/shared_preferences/shared_preferences_windows/test/shared_preferences_windows_test.dart @@ -10,8 +10,8 @@ import 'package:path_provider_windows/path_provider_windows.dart'; import 'package:shared_preferences_windows/shared_preferences_windows.dart'; void main() { - late MemoryFileSystem fileSystem; - late PathProviderWindows pathProvider; + MemoryFileSystem fileSystem; + PathProviderWindows pathProvider; setUp(() { fileSystem = MemoryFileSystem.test(); @@ -22,7 +22,7 @@ void main() { Future _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 { @@ -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 { - late VersionInfoQuerier versionInfoQuerier; + VersionInfoQuerier versionInfoQuerier; @override - Future getApplicationSupportPath() async => r'C:\appsupport'; + Future getApplicationSupportPath() async => r'C:\appsupport'; @override - Future getTemporaryPath() async => null; + Future getTemporaryPath() async => null; @override - Future getLibraryPath() async => null; + Future getLibraryPath() async => null; @override - Future getApplicationDocumentsPath() async => null; + Future getApplicationDocumentsPath() async => null; @override - Future getDownloadsPath() async => null; + Future getDownloadsPath() async => null; @override - Future getPath(String folderID) async => ''; + Future getPath(String folderID) async => null; } From 286637c492bf11af9d248eae28c777bc9e1d0396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan?= Date: Mon, 8 Feb 2021 18:13:53 +0100 Subject: [PATCH 11/12] Fixes --- .../shared_preferences_test.dart | 87 ++++++++++--------- .../example/pubspec.yaml | 5 -- .../lib/shared_preferences_linux.dart | 10 ++- .../shared_preferences_linux/pubspec.yaml | 2 +- .../shared_preferences_web/pubspec.yaml | 2 +- .../test/shared_preferences_web_test.dart | 5 ++ 6 files changed, 62 insertions(+), 49 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_linux/example/integration_test/shared_preferences_test.dart b/packages/shared_preferences/shared_preferences_linux/example/integration_test/shared_preferences_test.dart index e43d4e3ae0c2..3aedccd0feba 100644 --- a/packages/shared_preferences/shared_preferences_linux/example/integration_test/shared_preferences_test.dart +++ b/packages/shared_preferences/shared_preferences_linux/example/integration_test/shared_preferences_test.dart @@ -1,12 +1,13 @@ import 'dart:async'; + import 'package:flutter_test/flutter_test.dart'; -import 'package:shared_preferences/shared_preferences.dart'; import 'package:integration_test/integration_test.dart'; +import 'package:shared_preferences_linux/shared_preferences_linux.dart'; void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); - group('$SharedPreferences', () { + group('SharedPreferencesLinux', () { const Map kTestValues = { 'flutter.String': 'hello world', 'flutter.bool': true, @@ -23,10 +24,10 @@ void main() { 'flutter.List': ['baz', 'quox'], }; - SharedPreferences preferences; + SharedPreferencesLinux preferences; setUp(() async { - preferences = await SharedPreferences.getInstance(); + preferences = SharedPreferencesLinux.instance; }); tearDown(() { @@ -34,56 +35,62 @@ void main() { }); testWidgets('reading', (WidgetTester _) async { - expect(preferences.get('String'), isNull); - expect(preferences.get('bool'), isNull); - expect(preferences.get('int'), isNull); - expect(preferences.get('double'), isNull); - expect(preferences.get('List'), isNull); - expect(preferences.getString('String'), isNull); - expect(preferences.getBool('bool'), isNull); - expect(preferences.getInt('int'), isNull); - expect(preferences.getDouble('double'), isNull); - expect(preferences.getStringList('List'), isNull); + final all = await preferences.getAll(); + expect(all['String'], isNull); + expect(all['bool'], isNull); + expect(all['int'], isNull); + expect(all['double'], isNull); + expect(all['List'], isNull); }); testWidgets('writing', (WidgetTester _) async { await Future.wait(>[ - preferences.setString('String', kTestValues2['flutter.String']), - preferences.setBool('bool', kTestValues2['flutter.bool']), - preferences.setInt('int', kTestValues2['flutter.int']), - preferences.setDouble('double', kTestValues2['flutter.double']), - preferences.setStringList('List', kTestValues2['flutter.List']) + preferences.setValue( + 'String', 'String', kTestValues2['flutter.String']), + preferences.setValue('Bool', 'bool', kTestValues2['flutter.bool']), + preferences.setValue('Int', 'int', kTestValues2['flutter.int']), + preferences.setValue( + 'Double', 'double', kTestValues2['flutter.double']), + preferences.setValue('StringList', 'List', kTestValues2['flutter.List']) ]); - expect(preferences.getString('String'), kTestValues2['flutter.String']); - expect(preferences.getBool('bool'), kTestValues2['flutter.bool']); - expect(preferences.getInt('int'), kTestValues2['flutter.int']); - expect(preferences.getDouble('double'), kTestValues2['flutter.double']); - expect(preferences.getStringList('List'), kTestValues2['flutter.List']); + final all = await preferences.getAll(); + expect(all['String'], kTestValues2['flutter.String']); + expect(all['bool'], kTestValues2['flutter.bool']); + expect(all['int'], kTestValues2['flutter.int']); + expect(all['double'], kTestValues2['flutter.double']); + expect(all['List'], kTestValues2['flutter.List']); }); testWidgets('removing', (WidgetTester _) async { const String key = 'testKey'; - await preferences.setString(key, kTestValues['flutter.String']); - await preferences.setBool(key, kTestValues['flutter.bool']); - await preferences.setInt(key, kTestValues['flutter.int']); - await preferences.setDouble(key, kTestValues['flutter.double']); - await preferences.setStringList(key, kTestValues['flutter.List']); + + await Future.wait([ + preferences.setValue('String', key, kTestValues['flutter.String']), + preferences.setValue('Bool', key, kTestValues['flutter.bool']), + preferences.setValue('Int', key, kTestValues['flutter.int']), + preferences.setValue('Double', key, kTestValues['flutter.double']), + preferences.setValue('StringList', key, kTestValues['flutter.List']) + ]); await preferences.remove(key); - expect(preferences.get('testKey'), isNull); + final all = await preferences.getAll(); + expect(all['testKey'], isNull); }); testWidgets('clearing', (WidgetTester _) async { - await preferences.setString('String', kTestValues['flutter.String']); - await preferences.setBool('bool', kTestValues['flutter.bool']); - await preferences.setInt('int', kTestValues['flutter.int']); - await preferences.setDouble('double', kTestValues['flutter.double']); - await preferences.setStringList('List', kTestValues['flutter.List']); + await Future.wait(>[ + preferences.setValue('String', 'String', kTestValues['flutter.String']), + preferences.setValue('Bool', 'bool', kTestValues['flutter.bool']), + preferences.setValue('Int', 'int', kTestValues['flutter.int']), + preferences.setValue('Double', 'double', kTestValues['flutter.double']), + preferences.setValue('StringList', 'List', kTestValues['flutter.List']) + ]); await preferences.clear(); - expect(preferences.getString('String'), null); - expect(preferences.getBool('bool'), null); - expect(preferences.getInt('int'), null); - expect(preferences.getDouble('double'), null); - expect(preferences.getStringList('List'), null); + final all = await preferences.getAll(); + expect(all['String'], null); + expect(all['bool'], null); + expect(all['int'], null); + expect(all['double'], null); + expect(all['List'], null); }); }); } diff --git a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml index 3925205e2f2a..591aad4c2c57 100644 --- a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml @@ -4,13 +4,8 @@ description: Demonstrates how to use the shared_preferences_linux plugin. dependencies: flutter: sdk: flutter - shared_preferences: any - -dependency_overrides: shared_preferences_linux: path: ../ - shared_preferences_platform_interface: - path: ../../shared_preferences_platform_interface dev_dependencies: flutter_driver: diff --git a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart index 16f61293fcf5..1e06d7cc5378 100644 --- a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart +++ b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart @@ -27,10 +27,11 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { FileSystem fs = LocalFileSystem(); /// Gets the file where the preferences are stored. - Future _getLocalDataFile() async { + Future _getLocalDataFile() async { final pathProvider = PathProviderLinux(); final directory = await pathProvider.getApplicationSupportPath(); - return fs.file(path.join(directory!, 'shared_preferences.json')); + if (directory == null) return null; + return fs.file(path.join(directory, 'shared_preferences.json')); } /// Gets the preferences from the stored file. Once read, the preferences are @@ -41,6 +42,10 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { } var localDataFile = await _getLocalDataFile(); + if (localDataFile == null) { + _cachedPreferences ??= {}; + return _cachedPreferences!; + } if (localDataFile.existsSync()) { String stringMap = localDataFile.readAsStringSync(); if (stringMap.isNotEmpty) { @@ -58,6 +63,7 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { Future _writePreferences(Map preferences) async { try { var localDataFile = await _getLocalDataFile(); + if (localDataFile == null) return false; if (!localDataFile.existsSync()) { localDataFile.createSync(recursive: true); } diff --git a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml index 9a51976aafaf..df4b5db23b7f 100644 --- a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml @@ -11,7 +11,7 @@ flutter: pluginClass: none environment: - sdk: ">=2.12.0-259.8.beta <3.0.0" + sdk: ">=2.12.0-0 <3.0.0" flutter: ">=1.12.8" dependencies: diff --git a/packages/shared_preferences/shared_preferences_web/pubspec.yaml b/packages/shared_preferences/shared_preferences_web/pubspec.yaml index eb124105d9b6..60892bcf277c 100644 --- a/packages/shared_preferences/shared_preferences_web/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_web/pubspec.yaml @@ -27,5 +27,5 @@ dev_dependencies: pedantic: ^1.8.0 environment: - sdk: ">=2.12.0-259.8.beta <3.0.0" + sdk: ">=2.12.0-0 <3.0.0" flutter: ">=1.12.13+hotfix.4" diff --git a/packages/shared_preferences/shared_preferences_web/test/shared_preferences_web_test.dart b/packages/shared_preferences/shared_preferences_web/test/shared_preferences_web_test.dart index 9dab5665da22..c0cf92cc1bf0 100644 --- a/packages/shared_preferences/shared_preferences_web/test/shared_preferences_web_test.dart +++ b/packages/shared_preferences/shared_preferences_web/test/shared_preferences_web_test.dart @@ -7,6 +7,7 @@ import 'dart:convert' show json; import 'dart:html' as html; import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences_platform_interface/method_channel_shared_preferences.dart'; import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; import 'package:shared_preferences_web/shared_preferences_web.dart'; @@ -25,6 +26,10 @@ void main() { }); test('registers itself', () { + SharedPreferencesStorePlatform.instance = + MethodChannelSharedPreferencesStore(); + expect(SharedPreferencesStorePlatform.instance, + isNot(isA())); SharedPreferencesPlugin.registerWith(null); expect(SharedPreferencesStorePlatform.instance, isA()); From 92577c9c86cab05a0de4e7757695dd5341e121df Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Mon, 8 Feb 2021 10:30:56 -0800 Subject: [PATCH 12/12] Adjust read/write to match Windows --- .../lib/shared_preferences_linux.dart | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart index 1e06d7cc5378..5a694658cdf5 100644 --- a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart +++ b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart @@ -41,21 +41,16 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { return _cachedPreferences!; } - var localDataFile = await _getLocalDataFile(); - if (localDataFile == null) { - _cachedPreferences ??= {}; - return _cachedPreferences!; - } - if (localDataFile.existsSync()) { + Map preferences = {}; + final File? localDataFile = await _getLocalDataFile(); + if (localDataFile != null && localDataFile.existsSync()) { String stringMap = localDataFile.readAsStringSync(); if (stringMap.isNotEmpty) { - _cachedPreferences = - (json.decode(stringMap) as Map).cast(); + preferences = json.decode(stringMap).cast(); } } - - _cachedPreferences ??= {}; - return _cachedPreferences!; + _cachedPreferences = preferences; + return preferences; } /// Writes the cached preferences to disk. Returns [true] if the operation @@ -63,7 +58,10 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { Future _writePreferences(Map preferences) async { try { var localDataFile = await _getLocalDataFile(); - if (localDataFile == null) return false; + if (localDataFile == null) { + print("Unable to determine where to write preferences."); + return false; + } if (!localDataFile.existsSync()) { localDataFile.createSync(recursive: true); }