From bfdc06e843cf1d61d00989fcb70955e761774ab2 Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Wed, 20 Oct 2021 19:53:05 -0400 Subject: [PATCH] [shared_preferences] Switch to new analysis options (#4384) --- .../shared_preferences/analysis_options.yaml | 1 - .../shared_preferences/CHANGELOG.md | 4 + .../shared_preferences_test.dart | 91 +++++++-------- .../shared_preferences/example/lib/main.dart | 12 +- .../lib/shared_preferences.dart | 5 +- .../test/shared_preferences_test.dart | 107 ++++++++++-------- .../shared_preferences_linux/CHANGELOG.md | 4 + .../shared_preferences_test.dart | 47 ++++---- .../example/lib/main.dart | 12 +- .../lib/shared_preferences_linux.dart | 37 +++--- .../shared_preferences_linux/pubspec.yaml | 2 +- .../test/shared_preferences_linux_test.dart | 18 +-- .../shared_preferences_macos/CHANGELOG.md | 4 + .../shared_preferences_test.dart | 37 +++--- .../example/lib/main.dart | 8 +- .../example/pubspec.yaml | 2 +- .../CHANGELOG.md | 4 + .../method_channel_shared_preferences.dart | 4 +- ...ethod_channel_shared_preferences_test.dart | 14 +-- .../shared_preferences_web/CHANGELOG.md | 4 + .../shared_preferences_web_test.dart | 4 +- .../example/lib/main.dart | 2 +- .../example/pubspec.yaml | 10 +- .../lib/shared_preferences_web.dart | 14 +-- .../shared_preferences_windows/CHANGELOG.md | 4 + .../shared_preferences_test.dart | 38 ++++--- .../example/lib/main.dart | 12 +- .../lib/shared_preferences_windows.dart | 32 +++--- .../shared_preferences_windows/pubspec.yaml | 2 +- .../test/shared_preferences_windows_test.dart | 17 +-- script/configs/custom_analysis.yaml | 1 - 31 files changed, 296 insertions(+), 257 deletions(-) delete mode 100644 packages/shared_preferences/analysis_options.yaml diff --git a/packages/shared_preferences/analysis_options.yaml b/packages/shared_preferences/analysis_options.yaml deleted file mode 100644 index cda4f6e153e6..000000000000 --- a/packages/shared_preferences/analysis_options.yaml +++ /dev/null @@ -1 +0,0 @@ -include: ../../analysis_options_legacy.yaml diff --git a/packages/shared_preferences/shared_preferences/CHANGELOG.md b/packages/shared_preferences/shared_preferences/CHANGELOG.md index db12fd1829aa..5b9179664385 100644 --- a/packages/shared_preferences/shared_preferences/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences/CHANGELOG.md @@ -1,3 +1,7 @@ +## NEXT + +* Fixes newly enabled analyzer options. + ## 2.0.8 * Update minimum Flutter SDK to 2.5 and iOS deployment target to 9.0. diff --git a/packages/shared_preferences/shared_preferences/example/integration_test/shared_preferences_test.dart b/packages/shared_preferences/shared_preferences/example/integration_test/shared_preferences_test.dart index e8498f473a2c..fafdc18ec8b1 100644 --- a/packages/shared_preferences/shared_preferences/example/integration_test/shared_preferences_test.dart +++ b/packages/shared_preferences/shared_preferences/example/integration_test/shared_preferences_test.dart @@ -4,30 +4,27 @@ import 'dart:async'; import 'dart:io'; + import 'package:flutter/services.dart'; 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/shared_preferences.dart'; void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); group('$SharedPreferences', () { - const Map kTestValues = { - 'flutter.String': 'hello world', - 'flutter.bool': true, - 'flutter.int': 42, - 'flutter.double': 3.14159, - 'flutter.List': ['foo', 'bar'], - }; + const String testString = 'hello world'; + const bool testBool = true; + const int testInt = 42; + const double testDouble = 3.14159; + const List testList = ['foo', 'bar']; - const Map kTestValues2 = { - 'flutter.String': 'goodbye world', - 'flutter.bool': false, - 'flutter.int': 1337, - 'flutter.double': 2.71828, - 'flutter.List': ['baz', 'quox'], - }; + const String testString2 = 'goodbye world'; + const bool testBool2 = false; + const int testInt2 = 1337; + const double testDouble2 = 2.71828; + const List testList2 = ['baz', 'quox']; late SharedPreferences preferences; @@ -54,36 +51,36 @@ void main() { 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.setString('String', testString2), + preferences.setBool('bool', testBool2), + preferences.setInt('int', testInt2), + preferences.setDouble('double', testDouble2), + preferences.setStringList('List', testList2) ]); - 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']); + expect(preferences.getString('String'), testString2); + expect(preferences.getBool('bool'), testBool2); + expect(preferences.getInt('int'), testInt2); + expect(preferences.getDouble('double'), testDouble2); + expect(preferences.getStringList('List'), testList2); }); 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 preferences.setString(key, testString); + await preferences.setBool(key, testBool); + await preferences.setInt(key, testInt); + await preferences.setDouble(key, testDouble); + await preferences.setStringList(key, testList); await preferences.remove(key); expect(preferences.get('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 preferences.setString('String', testString); + await preferences.setBool('bool', testBool); + await preferences.setInt('int', testInt); + await preferences.setDouble('double', testDouble); + await preferences.setStringList('List', testList); await preferences.clear(); expect(preferences.getString('String'), null); expect(preferences.getBool('bool'), null); @@ -94,13 +91,13 @@ void main() { testWidgets('simultaneous writes', (WidgetTester _) async { final List> writes = >[]; - final int writeCount = 100; + const int writeCount = 100; for (int i = 1; i <= writeCount; i++) { writes.add(preferences.setInt('int', i)); } - List result = await Future.wait(writes, eagerError: true); + final List result = await Future.wait(writes, eagerError: true); // All writes should succeed. - expect(result.where((element) => !element), isEmpty); + expect(result.where((bool element) => !element), isEmpty); // The last write should win. expect(preferences.getInt('int'), writeCount); }); @@ -112,28 +109,22 @@ void main() { // special prefixes plus a string value expect( // prefix for lists - preferences.setString( - 'String', - 'VGhpcyBpcyB0aGUgcHJlZml4IGZvciBhIGxpc3Qu' + - kTestValues2['flutter.String']), + preferences.setString('String', + 'VGhpcyBpcyB0aGUgcHJlZml4IGZvciBhIGxpc3Qu' + testString), throwsA(isA())); await preferences.reload(); expect(preferences.getString('String'), null); expect( // prefix for big integers - preferences.setString( - 'String', - 'VGhpcyBpcyB0aGUgcHJlZml4IGZvciBCaWdJbnRlZ2Vy' + - kTestValues2['flutter.String']), + preferences.setString('String', + 'VGhpcyBpcyB0aGUgcHJlZml4IGZvciBCaWdJbnRlZ2Vy' + testString), throwsA(isA())); await preferences.reload(); expect(preferences.getString('String'), null); expect( // prefix for doubles - preferences.setString( - 'String', - 'VGhpcyBpcyB0aGUgcHJlZml4IGZvciBEb3VibGUu' + - kTestValues2['flutter.String']), + preferences.setString('String', + 'VGhpcyBpcyB0aGUgcHJlZml4IGZvciBEb3VibGUu' + testString), throwsA(isA())); await preferences.reload(); expect(preferences.getString('String'), null); diff --git a/packages/shared_preferences/shared_preferences/example/lib/main.dart b/packages/shared_preferences/shared_preferences/example/lib/main.dart index 103481a2d295..43f3c78ad920 100644 --- a/packages/shared_preferences/shared_preferences/example/lib/main.dart +++ b/packages/shared_preferences/shared_preferences/example/lib/main.dart @@ -16,7 +16,7 @@ void main() { class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return MaterialApp( + return const MaterialApp( title: 'SharedPreferences Demo', home: SharedPreferencesDemo(), ); @@ -24,14 +24,14 @@ class MyApp extends StatelessWidget { } class SharedPreferencesDemo extends StatefulWidget { - SharedPreferencesDemo({Key? key}) : super(key: key); + const SharedPreferencesDemo({Key? key}) : super(key: key); @override SharedPreferencesDemoState createState() => SharedPreferencesDemoState(); } class SharedPreferencesDemoState extends State { - Future _prefs = SharedPreferences.getInstance(); + final Future _prefs = SharedPreferences.getInstance(); late Future _counter; Future _incrementCounter() async { @@ -39,7 +39,7 @@ class SharedPreferencesDemoState extends State { final int counter = (prefs.getInt('counter') ?? 0) + 1; setState(() { - _counter = prefs.setInt("counter", counter).then((bool success) { + _counter = prefs.setInt('counter', counter).then((bool success) { return counter; }); }); @@ -49,7 +49,7 @@ class SharedPreferencesDemoState extends State { void initState() { super.initState(); _counter = _prefs.then((SharedPreferences prefs) { - return (prefs.getInt('counter') ?? 0); + return prefs.getInt('counter') ?? 0; }); } @@ -57,7 +57,7 @@ class SharedPreferencesDemoState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: const Text("SharedPreferences Demo"), + title: const Text('SharedPreferences Demo'), ), body: Center( child: FutureBuilder( diff --git a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart index 841d615262de..b0a11fc8b13a 100644 --- a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart +++ b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart @@ -50,7 +50,8 @@ class SharedPreferences { /// performance-sensitive blocks. static Future getInstance() async { if (_completer == null) { - final completer = Completer(); + final Completer completer = + Completer(); try { final Map preferencesMap = await _getSharedPreferencesMap(); @@ -188,7 +189,7 @@ class SharedPreferences { assert(fromSystem != null); // Strip the flutter. prefix from the returned preferences. final Map preferencesMap = {}; - for (String key in fromSystem.keys) { + for (final String key in fromSystem.keys) { assert(key.startsWith(_prefix)); preferencesMap[key.substring(_prefix.length)] = fromSystem[key]!; } diff --git a/packages/shared_preferences/shared_preferences/test/shared_preferences_test.dart b/packages/shared_preferences/shared_preferences/test/shared_preferences_test.dart index 878021a03361..1938a04ae84b 100755 --- a/packages/shared_preferences/shared_preferences/test/shared_preferences_test.dart +++ b/packages/shared_preferences/shared_preferences/test/shared_preferences_test.dart @@ -11,27 +11,37 @@ void main() { TestWidgetsFlutterBinding.ensureInitialized(); group('SharedPreferences', () { - const Map kTestValues = { - 'flutter.String': 'hello world', - 'flutter.bool': true, - 'flutter.int': 42, - 'flutter.double': 3.14159, - 'flutter.List': ['foo', 'bar'], + const String testString = 'hello world'; + const bool testBool = true; + const int testInt = 42; + const double testDouble = 3.14159; + const List testList = ['foo', 'bar']; + const Map testValues = { + 'flutter.String': testString, + 'flutter.bool': testBool, + 'flutter.int': testInt, + 'flutter.double': testDouble, + 'flutter.List': testList, }; - const Map kTestValues2 = { - 'flutter.String': 'goodbye world', - 'flutter.bool': false, - 'flutter.int': 1337, - 'flutter.double': 2.71828, - 'flutter.List': ['baz', 'quox'], + const String testString2 = 'goodbye world'; + const bool testBool2 = false; + const int testInt2 = 1337; + const double testDouble2 = 2.71828; + const List testList2 = ['baz', 'quox']; + const Map testValues2 = { + 'flutter.String': testString2, + 'flutter.bool': testBool2, + 'flutter.int': testInt2, + 'flutter.double': testDouble2, + 'flutter.List': testList2, }; late FakeSharedPreferencesStore store; late SharedPreferences preferences; setUp(() async { - store = FakeSharedPreferencesStore(kTestValues); + store = FakeSharedPreferencesStore(testValues); SharedPreferencesStorePlatform.instance = store; preferences = await SharedPreferences.getInstance(); store.log.clear(); @@ -43,26 +53,26 @@ void main() { }); test('reading', () async { - expect(preferences.get('String'), kTestValues['flutter.String']); - expect(preferences.get('bool'), kTestValues['flutter.bool']); - expect(preferences.get('int'), kTestValues['flutter.int']); - expect(preferences.get('double'), kTestValues['flutter.double']); - expect(preferences.get('List'), kTestValues['flutter.List']); - expect(preferences.getString('String'), kTestValues['flutter.String']); - expect(preferences.getBool('bool'), kTestValues['flutter.bool']); - expect(preferences.getInt('int'), kTestValues['flutter.int']); - expect(preferences.getDouble('double'), kTestValues['flutter.double']); - expect(preferences.getStringList('List'), kTestValues['flutter.List']); + expect(preferences.get('String'), testString); + expect(preferences.get('bool'), testBool); + expect(preferences.get('int'), testInt); + expect(preferences.get('double'), testDouble); + expect(preferences.get('List'), testList); + expect(preferences.getString('String'), testString); + expect(preferences.getBool('bool'), testBool); + expect(preferences.getInt('int'), testInt); + expect(preferences.getDouble('double'), testDouble); + expect(preferences.getStringList('List'), testList); expect(store.log, []); }); test('writing', () 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.setString('String', testString2), + preferences.setBool('bool', testBool2), + preferences.setInt('int', testInt2), + preferences.setDouble('double', testDouble2), + preferences.setStringList('List', testList2) ]); expect( store.log, @@ -70,37 +80,37 @@ void main() { isMethodCall('setValue', arguments: [ 'String', 'flutter.String', - kTestValues2['flutter.String'], + testString2, ]), isMethodCall('setValue', arguments: [ 'Bool', 'flutter.bool', - kTestValues2['flutter.bool'], + testBool2, ]), isMethodCall('setValue', arguments: [ 'Int', 'flutter.int', - kTestValues2['flutter.int'], + testInt2, ]), isMethodCall('setValue', arguments: [ 'Double', 'flutter.double', - kTestValues2['flutter.double'], + testDouble2, ]), isMethodCall('setValue', arguments: [ 'StringList', 'flutter.List', - kTestValues2['flutter.List'], + testList2, ]), ], ); store.log.clear(); - 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']); + expect(preferences.getString('String'), testString2); + expect(preferences.getBool('bool'), testBool2); + expect(preferences.getInt('int'), testInt2); + expect(preferences.getDouble('double'), testDouble2); + expect(preferences.getStringList('List'), testList2); expect(store.log, equals([])); }); @@ -139,16 +149,15 @@ void main() { }); test('reloading', () async { - await preferences.setString( - 'String', kTestValues['flutter.String'] as String); - expect(preferences.getString('String'), kTestValues['flutter.String']); + await preferences.setString('String', testString); + expect(preferences.getString('String'), testString); SharedPreferences.setMockInitialValues( - kTestValues2.cast()); - expect(preferences.getString('String'), kTestValues['flutter.String']); + testValues2.cast()); + expect(preferences.getString('String'), testString); await preferences.reload(); - expect(preferences.getString('String'), kTestValues2['flutter.String']); + expect(preferences.getString('String'), testString2); }); test('back to back calls should return same instance.', () async { @@ -189,13 +198,13 @@ void main() { test('writing copy of strings list', () async { final List myList = []; - await preferences.setStringList("myList", myList); - myList.add("foobar"); + await preferences.setStringList('myList', myList); + myList.add('foobar'); final List cachedList = preferences.getStringList('myList')!; expect(cachedList, []); - cachedList.add("foobar2"); + cachedList.add('foobar2'); expect(preferences.getStringList('myList'), []); }); @@ -223,13 +232,13 @@ class FakeSharedPreferencesStore implements SharedPreferencesStorePlatform { @override Future clear() { - log.add(MethodCall('clear')); + log.add(const MethodCall('clear')); return backend.clear(); } @override Future> getAll() { - log.add(MethodCall('getAll')); + log.add(const MethodCall('getAll')); return backend.getAll(); } diff --git a/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md index fc09bec23591..6fd14eebc7f0 100644 --- a/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md @@ -1,3 +1,7 @@ +## NEXT + +* Fixes newly enabled analyzer options. + ## 2.0.2 * Updated installation instructions in README. 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 5dba3def31d0..b33d6011f5c4 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 @@ -12,7 +12,7 @@ void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); group('SharedPreferencesLinux', () { - const Map kTestValues = { + const Map kTestValues = { 'flutter.String': 'hello world', 'flutter.bool': true, 'flutter.int': 42, @@ -20,7 +20,7 @@ void main() { 'flutter.List': ['foo', 'bar'], }; - const Map kTestValues2 = { + const Map kTestValues2 = { 'flutter.String': 'goodbye world', 'flutter.bool': false, 'flutter.int': 1337, @@ -39,7 +39,7 @@ void main() { }); testWidgets('reading', (WidgetTester _) async { - final all = await preferences.getAll(); + final Map all = await preferences.getAll(); expect(all['String'], isNull); expect(all['bool'], isNull); expect(all['int'], isNull); @@ -50,14 +50,15 @@ void main() { testWidgets('writing', (WidgetTester _) async { await Future.wait(>[ preferences.setValue( - 'String', 'String', kTestValues2['flutter.String']), - preferences.setValue('Bool', 'bool', kTestValues2['flutter.bool']), - preferences.setValue('Int', 'int', kTestValues2['flutter.int']), + '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']) + 'Double', 'double', kTestValues2['flutter.double']!), + preferences.setValue( + 'StringList', 'List', kTestValues2['flutter.List']!) ]); - final all = await preferences.getAll(); + final Map all = await preferences.getAll(); expect(all['String'], kTestValues2['flutter.String']); expect(all['bool'], kTestValues2['flutter.bool']); expect(all['int'], kTestValues2['flutter.int']); @@ -68,28 +69,30 @@ void main() { testWidgets('removing', (WidgetTester _) async { const String key = 'testKey'; - 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 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); - final all = await preferences.getAll(); + final Map all = await preferences.getAll(); expect(all['testKey'], isNull); }); testWidgets('clearing', (WidgetTester _) async { 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']) + 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(); - final all = await preferences.getAll(); + final Map all = await preferences.getAll(); expect(all['String'], null); expect(all['bool'], null); expect(all['int'], null); diff --git a/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart b/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart index aee71d00d44d..b4a7ea3f2c1e 100644 --- a/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart +++ b/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart @@ -16,7 +16,7 @@ void main() { class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return MaterialApp( + return const MaterialApp( title: 'SharedPreferences Demo', home: SharedPreferencesDemo(), ); @@ -24,18 +24,18 @@ class MyApp extends StatelessWidget { } class SharedPreferencesDemo extends StatefulWidget { - SharedPreferencesDemo({Key? key}) : super(key: key); + const SharedPreferencesDemo({Key? key}) : super(key: key); @override SharedPreferencesDemoState createState() => SharedPreferencesDemoState(); } class SharedPreferencesDemoState extends State { - final prefs = SharedPreferencesLinux.instance; + final SharedPreferencesLinux prefs = SharedPreferencesLinux.instance; late Future _counter; Future _incrementCounter() async { - final values = await prefs.getAll(); + final Map values = await prefs.getAll(); final int counter = (values['counter'] as int? ?? 0) + 1; setState(() { @@ -49,7 +49,7 @@ class SharedPreferencesDemoState extends State { void initState() { super.initState(); _counter = prefs.getAll().then((Map values) { - return (values['counter'] as int? ?? 0); + return values['counter'] as int? ?? 0; }); } @@ -57,7 +57,7 @@ class SharedPreferencesDemoState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: const Text("SharedPreferences Demo"), + title: const Text('SharedPreferences Demo'), ), body: Center( child: FutureBuilder( 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 5ec988216074..cf887ccb7ad1 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 @@ -17,8 +17,8 @@ import 'package:shared_preferences_platform_interface/shared_preferences_platfor /// This class implements the `package:shared_preferences` functionality for Linux. class SharedPreferencesLinux extends SharedPreferencesStorePlatform { /// The default instance of [SharedPreferencesLinux] to use. - /// TODO(egarciad): Remove when the Dart plugin registrant lands on Flutter stable. - /// https://github.com/flutter/flutter/issues/81421 + // TODO(egarciad): Remove when the Dart plugin registrant lands on Flutter stable. + // https://github.com/flutter/flutter/issues/81421 static SharedPreferencesLinux instance = SharedPreferencesLinux(); /// Registers the Linux implementation. @@ -31,13 +31,15 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { /// File system used to store to disk. Exposed for testing only. @visibleForTesting - FileSystem fs = LocalFileSystem(); + FileSystem fs = const LocalFileSystem(); /// Gets the file where the preferences are stored. Future _getLocalDataFile() async { - final pathProvider = PathProviderLinux(); - final directory = await pathProvider.getApplicationSupportPath(); - if (directory == null) return null; + final PathProviderLinux pathProvider = PathProviderLinux(); + final String? directory = await pathProvider.getApplicationSupportPath(); + if (directory == null) { + return null; + } return fs.file(path.join(directory, 'shared_preferences.json')); } @@ -48,12 +50,15 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { return _cachedPreferences!; } - Map preferences = {}; + Map preferences = {}; final File? localDataFile = await _getLocalDataFile(); if (localDataFile != null && localDataFile.existsSync()) { - String stringMap = localDataFile.readAsStringSync(); + final String stringMap = localDataFile.readAsStringSync(); if (stringMap.isNotEmpty) { - preferences = json.decode(stringMap).cast(); + final Object? data = json.decode(stringMap); + if (data is Map) { + preferences = data.cast(); + } } } _cachedPreferences = preferences; @@ -64,18 +69,18 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { /// succeeded. Future _writePreferences(Map preferences) async { try { - var localDataFile = await _getLocalDataFile(); + final File? localDataFile = await _getLocalDataFile(); if (localDataFile == null) { - print("Unable to determine where to write preferences."); + print('Unable to determine where to write preferences.'); return false; } if (!localDataFile.existsSync()) { localDataFile.createSync(recursive: true); } - var stringMap = json.encode(preferences); + final String stringMap = json.encode(preferences); localDataFile.writeAsStringSync(stringMap); } catch (e) { - print("Error saving preferences to disk: $e"); + print('Error saving preferences to disk: $e'); return false; } return true; @@ -83,7 +88,7 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { @override Future clear() async { - var preferences = await _readPreferences(); + final Map preferences = await _readPreferences(); preferences.clear(); return _writePreferences(preferences); } @@ -95,14 +100,14 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { @override Future remove(String key) async { - var preferences = await _readPreferences(); + final Map preferences = await _readPreferences(); preferences.remove(key); return _writePreferences(preferences); } @override Future setValue(String valueType, String key, Object value) async { - var preferences = await _readPreferences(); + final Map preferences = await _readPreferences(); preferences[key] = value; return _writePreferences(preferences); } diff --git a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml index c03e49e042e2..b6d1765c08f3 100644 --- a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml @@ -18,9 +18,9 @@ flutter: dependencies: file: ^6.0.0 - meta: ^1.3.0 flutter: sdk: flutter + meta: ^1.3.0 path: ^1.8.0 path_provider_linux: ^2.0.0 shared_preferences_platform_interface: ^2.0.0 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 62ec2b66c07a..40dbdd58afc1 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 @@ -18,12 +18,12 @@ void main() { }); Future _getFilePath() async { - final pathProvider = PathProviderLinux(); - final directory = await pathProvider.getApplicationSupportPath(); + final PathProviderLinux pathProvider = PathProviderLinux(); + final String? directory = await pathProvider.getApplicationSupportPath(); return path.join(directory!, 'shared_preferences.json'); } - _writeTestFile(String value) async { + Future _writeTestFile(String value) async { fs.file(await _getFilePath()) ..createSync(recursive: true) ..writeAsStringSync(value); @@ -34,7 +34,7 @@ void main() { } SharedPreferencesLinux _getPreferences() { - var prefs = SharedPreferencesLinux(); + final SharedPreferencesLinux prefs = SharedPreferencesLinux(); prefs.fs = fs; return prefs; } @@ -46,9 +46,9 @@ void main() { test('getAll', () async { await _writeTestFile('{"key1": "one", "key2": 2}'); - var prefs = _getPreferences(); + final SharedPreferencesLinux prefs = _getPreferences(); - var values = await prefs.getAll(); + final Map values = await prefs.getAll(); expect(values, hasLength(2)); expect(values['key1'], 'one'); expect(values['key2'], 2); @@ -56,7 +56,7 @@ void main() { test('remove', () async { await _writeTestFile('{"key1":"one","key2":2}'); - var prefs = _getPreferences(); + final SharedPreferencesLinux prefs = _getPreferences(); await prefs.remove('key2'); @@ -65,7 +65,7 @@ void main() { test('setValue', () async { await _writeTestFile('{}'); - var prefs = _getPreferences(); + final SharedPreferencesLinux prefs = _getPreferences(); await prefs.setValue('', 'key1', 'one'); await prefs.setValue('', 'key2', 2); @@ -75,7 +75,7 @@ void main() { test('clear', () async { await _writeTestFile('{"key1":"one","key2":2}'); - var prefs = _getPreferences(); + final SharedPreferencesLinux prefs = _getPreferences(); await prefs.clear(); expect(await _readTestFile(), '{}'); diff --git a/packages/shared_preferences/shared_preferences_macos/CHANGELOG.md b/packages/shared_preferences/shared_preferences_macos/CHANGELOG.md index 2f7e0edf9a51..7a2e3ab74e72 100644 --- a/packages/shared_preferences/shared_preferences_macos/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences_macos/CHANGELOG.md @@ -1,3 +1,7 @@ +## NEXT + +* Fixes newly enabled analyzer options. + ## 2.0.2 * Add native unit tests. diff --git a/packages/shared_preferences/shared_preferences_macos/example/integration_test/shared_preferences_test.dart b/packages/shared_preferences/shared_preferences_macos/example/integration_test/shared_preferences_test.dart index e7a267f7e51a..66e3be30ee5d 100644 --- a/packages/shared_preferences/shared_preferences_macos/example/integration_test/shared_preferences_test.dart +++ b/packages/shared_preferences/shared_preferences_macos/example/integration_test/shared_preferences_test.dart @@ -3,15 +3,16 @@ // found in the LICENSE file. import 'dart:async'; + import 'package:flutter_test/flutter_test.dart'; -import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; import 'package:integration_test/integration_test.dart'; +import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); group('SharedPreferencesMacOS', () { - const Map kTestValues = { + const Map kTestValues = { 'flutter.String': 'hello world', 'flutter.bool': true, 'flutter.int': 42, @@ -19,7 +20,7 @@ void main() { 'flutter.List': ['foo', 'bar'], }; - const Map kTestValues2 = { + const Map kTestValues2 = { 'flutter.String': 'goodbye world', 'flutter.bool': false, 'flutter.int': 1337, @@ -55,15 +56,15 @@ void main() { testWidgets('writing', (WidgetTester _) async { await Future.wait(>[ preferences.setValue( - 'String', _prefixedKey('String'), kTestValues2['flutter.String']), + 'String', _prefixedKey('String'), kTestValues2['flutter.String']!), preferences.setValue( - 'Bool', _prefixedKey('bool'), kTestValues2['flutter.bool']), + 'Bool', _prefixedKey('bool'), kTestValues2['flutter.bool']!), preferences.setValue( - 'Int', _prefixedKey('int'), kTestValues2['flutter.int']), + 'Int', _prefixedKey('int'), kTestValues2['flutter.int']!), preferences.setValue( - 'Double', _prefixedKey('double'), kTestValues2['flutter.double']), + 'Double', _prefixedKey('double'), kTestValues2['flutter.double']!), preferences.setValue( - 'StringList', _prefixedKey('List'), kTestValues2['flutter.List']) + 'StringList', _prefixedKey('List'), kTestValues2['flutter.List']!) ]); final Map values = await preferences.getAll(); expect(values[_prefixedKey('String')], kTestValues2['flutter.String']); @@ -75,12 +76,12 @@ void main() { testWidgets('removing', (WidgetTester _) async { final String key = _prefixedKey('testKey'); - await preferences.setValue('String', key, kTestValues['flutter.String']); - await preferences.setValue('Bool', key, kTestValues['flutter.bool']); - await preferences.setValue('Int', key, kTestValues['flutter.int']); - await preferences.setValue('Double', key, kTestValues['flutter.double']); + await preferences.setValue('String', key, kTestValues['flutter.String']!); + await preferences.setValue('Bool', key, kTestValues['flutter.bool']!); + await preferences.setValue('Int', key, kTestValues['flutter.int']!); + await preferences.setValue('Double', key, kTestValues['flutter.double']!); await preferences.setValue( - 'StringList', key, kTestValues['flutter.List']); + 'StringList', key, kTestValues['flutter.List']!); await preferences.remove(key); final Map values = await preferences.getAll(); expect(values[key], isNull); @@ -88,13 +89,13 @@ void main() { testWidgets('clearing', (WidgetTester _) async { await preferences.setValue( - 'String', 'String', kTestValues['flutter.String']); - await preferences.setValue('Bool', 'bool', kTestValues['flutter.bool']); - await preferences.setValue('Int', 'int', kTestValues['flutter.int']); + 'String', 'String', kTestValues['flutter.String']!); + await preferences.setValue('Bool', 'bool', kTestValues['flutter.bool']!); + await preferences.setValue('Int', 'int', kTestValues['flutter.int']!); await preferences.setValue( - 'Double', 'double', kTestValues['flutter.double']); + 'Double', 'double', kTestValues['flutter.double']!); await preferences.setValue( - 'StringList', 'List', kTestValues['flutter.List']); + 'StringList', 'List', kTestValues['flutter.List']!); await preferences.clear(); final Map values = await preferences.getAll(); expect(values['String'], null); diff --git a/packages/shared_preferences/shared_preferences_macos/example/lib/main.dart b/packages/shared_preferences/shared_preferences_macos/example/lib/main.dart index fb85c301f623..349e9c45405a 100644 --- a/packages/shared_preferences/shared_preferences_macos/example/lib/main.dart +++ b/packages/shared_preferences/shared_preferences_macos/example/lib/main.dart @@ -16,7 +16,7 @@ void main() { class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return MaterialApp( + return const MaterialApp( title: 'SharedPreferences Demo', home: SharedPreferencesDemo(), ); @@ -24,14 +24,14 @@ class MyApp extends StatelessWidget { } class SharedPreferencesDemo extends StatefulWidget { - SharedPreferencesDemo({Key? key}) : super(key: key); + const SharedPreferencesDemo({Key? key}) : super(key: key); @override SharedPreferencesDemoState createState() => SharedPreferencesDemoState(); } class SharedPreferencesDemoState extends State { - SharedPreferencesStorePlatform _prefs = + final SharedPreferencesStorePlatform _prefs = SharedPreferencesStorePlatform.instance; late Future _counter; @@ -63,7 +63,7 @@ class SharedPreferencesDemoState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: const Text("SharedPreferences Demo"), + title: const Text('SharedPreferences Demo'), ), body: Center( child: FutureBuilder( diff --git a/packages/shared_preferences/shared_preferences_macos/example/pubspec.yaml b/packages/shared_preferences/shared_preferences_macos/example/pubspec.yaml index e6bf972cf5b4..d6f07f8eb2af 100644 --- a/packages/shared_preferences/shared_preferences_macos/example/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_macos/example/pubspec.yaml @@ -9,7 +9,6 @@ environment: dependencies: flutter: sdk: flutter - shared_preferences_platform_interface: ^2.0.0 shared_preferences_macos: # When depending on this package from a real application you should use: # shared_preferences_macos: ^x.y.z @@ -17,6 +16,7 @@ dependencies: # The example app is bundled with the plugin so we use a path dependency on # the parent directory to use the current plugin's version. path: ../ + shared_preferences_platform_interface: ^2.0.0 dev_dependencies: flutter_driver: diff --git a/packages/shared_preferences/shared_preferences_platform_interface/CHANGELOG.md b/packages/shared_preferences/shared_preferences_platform_interface/CHANGELOG.md index b402f6e57e88..51b59651b49a 100644 --- a/packages/shared_preferences/shared_preferences_platform_interface/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences_platform_interface/CHANGELOG.md @@ -1,3 +1,7 @@ +## NEXT + +* Fixes newly enabled analyzer options. + ## 2.0.0 * Migrate to null safety. diff --git a/packages/shared_preferences/shared_preferences_platform_interface/lib/method_channel_shared_preferences.dart b/packages/shared_preferences/shared_preferences_platform_interface/lib/method_channel_shared_preferences.dart index fa1bdc097b8d..2974f0a69e1b 100644 --- a/packages/shared_preferences/shared_preferences_platform_interface/lib/method_channel_shared_preferences.dart +++ b/packages/shared_preferences/shared_preferences_platform_interface/lib/method_channel_shared_preferences.dart @@ -43,7 +43,9 @@ class MethodChannelSharedPreferencesStore final Map? preferences = await _kChannel.invokeMapMethod('getAll'); - if (preferences == null) return {}; + if (preferences == null) { + return {}; + } return preferences; } } diff --git a/packages/shared_preferences/shared_preferences_platform_interface/test/method_channel_shared_preferences_test.dart b/packages/shared_preferences/shared_preferences_platform_interface/test/method_channel_shared_preferences_test.dart index 3b43062c2be3..31ee89e4c3f6 100644 --- a/packages/shared_preferences/shared_preferences_platform_interface/test/method_channel_shared_preferences_test.dart +++ b/packages/shared_preferences/shared_preferences_platform_interface/test/method_channel_shared_preferences_test.dart @@ -4,8 +4,8 @@ import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; import 'package:shared_preferences_platform_interface/method_channel_shared_preferences.dart'; +import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); @@ -37,7 +37,7 @@ void main() { return await testData.getAll(); } if (methodCall.method == 'remove') { - final String key = methodCall.arguments['key']; + final String key = (methodCall.arguments['key'] as String?)!; return await testData.remove(key); } if (methodCall.method == 'clear') { @@ -47,8 +47,8 @@ void main() { final Match? match = setterRegExp.matchAsPrefix(methodCall.method); if (match?.groupCount == 1) { final String valueType = match!.group(1)!; - final String key = methodCall.arguments['key']; - final Object value = methodCall.arguments['value']; + final String key = (methodCall.arguments['key'] as String?)!; + final Object value = (methodCall.arguments['value'] as Object?)!; return await testData.setValue(valueType, key, value); } fail('Unexpected method call: ${methodCall.method}'); @@ -78,15 +78,15 @@ void main() { }); expect(log, hasLength(4)); - for (MethodCall call in log) { + for (final MethodCall call in log) { expect(call.method, 'remove'); } }); test('setValue', () async { expect(await testData.getAll(), isEmpty); - for (String key in kTestValues.keys) { - final dynamic value = kTestValues[key]; + for (final String key in kTestValues.keys) { + final Object value = kTestValues[key]!; expect(await store.setValue(key.split('.').last, key, value), true); } expect(await testData.getAll(), kTestValues); diff --git a/packages/shared_preferences/shared_preferences_web/CHANGELOG.md b/packages/shared_preferences/shared_preferences_web/CHANGELOG.md index dd68f5321541..6332d01aa743 100644 --- a/packages/shared_preferences/shared_preferences_web/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences_web/CHANGELOG.md @@ -1,3 +1,7 @@ +## NEXT + +* Fixes newly enabled analyzer options. + ## 2.0.2 * Add `implements` to pubspec. diff --git a/packages/shared_preferences/shared_preferences_web/example/integration_test/shared_preferences_web_test.dart b/packages/shared_preferences/shared_preferences_web/example/integration_test/shared_preferences_web_test.dart index d95a0512615e..d3bfa49af8a0 100644 --- a/packages/shared_preferences/shared_preferences_web/example/integration_test/shared_preferences_web_test.dart +++ b/packages/shared_preferences/shared_preferences_web/example/integration_test/shared_preferences_web_test.dart @@ -62,12 +62,12 @@ void main() { testWidgets('setValue', (WidgetTester tester) async { final SharedPreferencesPlugin store = SharedPreferencesPlugin(); - for (String key in kTestValues.keys) { + for (final String key in kTestValues.keys) { final dynamic value = kTestValues[key]; expect(await store.setValue(key.split('.').last, key, value), true); } expect(html.window.localStorage.keys, hasLength(kTestValues.length)); - for (String key in html.window.localStorage.keys) { + for (final String key in html.window.localStorage.keys) { expect(html.window.localStorage[key], json.encode(kTestValues[key])); } diff --git a/packages/shared_preferences/shared_preferences_web/example/lib/main.dart b/packages/shared_preferences/shared_preferences_web/example/lib/main.dart index e1a38dcdcd46..341913a18490 100644 --- a/packages/shared_preferences/shared_preferences_web/example/lib/main.dart +++ b/packages/shared_preferences/shared_preferences_web/example/lib/main.dart @@ -17,7 +17,7 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { @override Widget build(BuildContext context) { - return Directionality( + return const Directionality( textDirection: TextDirection.ltr, child: Text('Testing... Look at the console output for results!'), ); diff --git a/packages/shared_preferences/shared_preferences_web/example/pubspec.yaml b/packages/shared_preferences/shared_preferences_web/example/pubspec.yaml index a83a71b40bf8..832ba912e5a8 100644 --- a/packages/shared_preferences/shared_preferences_web/example/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_web/example/pubspec.yaml @@ -6,16 +6,16 @@ environment: flutter: ">=2.2.0" dependencies: - shared_preferences_web: - path: ../ flutter: sdk: flutter + shared_preferences_web: + path: ../ dev_dependencies: - js: ^0.6.3 - flutter_test: - sdk: flutter flutter_driver: sdk: flutter + flutter_test: + sdk: flutter integration_test: sdk: flutter + js: ^0.6.3 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 9cff1d448896..d9d623465188 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 @@ -23,16 +23,14 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform { // IMPORTANT: Do not use html.window.localStorage.clear() as that will // remove _all_ local data, not just the keys prefixed with // "flutter." - for (String key in _storedFlutterKeys) { - html.window.localStorage.remove(key); - } + _storedFlutterKeys.forEach(html.window.localStorage.remove); return true; } @override Future> getAll() async { - final Map allData = {}; - for (String key in _storedFlutterKeys) { + final Map allData = {}; + for (final String key in _storedFlutterKeys) { allData[key] = _decodeValue(html.window.localStorage[key]!); } return allData; @@ -64,7 +62,7 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform { Iterable get _storedFlutterKeys { return html.window.localStorage.keys - .where((key) => key.startsWith('flutter.')); + .where((String key) => key.startsWith('flutter.')); } String _encodeValue(Object? value) { @@ -72,7 +70,7 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform { } Object _decodeValue(String encodedValue) { - final Object decodedValue = json.decode(encodedValue); + final Object? decodedValue = json.decode(encodedValue); if (decodedValue is List) { // JSON does not preserve generics. The encode/decode roundtrip is @@ -81,6 +79,6 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform { return decodedValue.cast(); } - return decodedValue; + return decodedValue!; } } diff --git a/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md b/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md index 7502ec917d80..68358a3c354e 100644 --- a/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md @@ -1,3 +1,7 @@ +## NEXT + +* Fixes newly enabled analyzer options. + ## 2.0.2 * Updated installation instructions in README. diff --git a/packages/shared_preferences/shared_preferences_windows/example/integration_test/shared_preferences_test.dart b/packages/shared_preferences/shared_preferences_windows/example/integration_test/shared_preferences_test.dart index 207d712650a7..ac138666518a 100644 --- a/packages/shared_preferences/shared_preferences_windows/example/integration_test/shared_preferences_test.dart +++ b/packages/shared_preferences/shared_preferences_windows/example/integration_test/shared_preferences_test.dart @@ -3,15 +3,16 @@ // found in the LICENSE file. import 'dart:async'; + import 'package:flutter_test/flutter_test.dart'; -import 'package:shared_preferences_windows/shared_preferences_windows.dart'; import 'package:integration_test/integration_test.dart'; +import 'package:shared_preferences_windows/shared_preferences_windows.dart'; void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); group('SharedPreferencesWindows', () { - const Map kTestValues = { + const Map kTestValues = { 'flutter.String': 'hello world', 'flutter.bool': true, 'flutter.int': 42, @@ -19,7 +20,7 @@ void main() { 'flutter.List': ['foo', 'bar'], }; - const Map kTestValues2 = { + const Map kTestValues2 = { 'flutter.String': 'goodbye world', 'flutter.bool': false, 'flutter.int': 1337, @@ -49,12 +50,13 @@ void main() { testWidgets('writing', (WidgetTester _) async { await Future.wait(>[ preferences.setValue( - 'String', 'String', kTestValues2['flutter.String']), - preferences.setValue('Bool', 'bool', kTestValues2['flutter.bool']), - preferences.setValue('Int', 'int', kTestValues2['flutter.int']), + '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( - 'Double', 'double', kTestValues2['flutter.double']), - preferences.setValue('StringList', 'List', kTestValues2['flutter.List']) + 'StringList', 'List', kTestValues2['flutter.List']!) ]); final Map values = await preferences.getAll(); expect(values['String'], kTestValues2['flutter.String']); @@ -66,12 +68,12 @@ void main() { testWidgets('removing', (WidgetTester _) async { const String key = 'testKey'; - await preferences.setValue('String', key, kTestValues['flutter.String']); - await preferences.setValue('Bool', key, kTestValues['flutter.bool']); - await preferences.setValue('Int', key, kTestValues['flutter.int']); - await preferences.setValue('Double', key, kTestValues['flutter.double']); + await preferences.setValue('String', key, kTestValues['flutter.String']!); + await preferences.setValue('Bool', key, kTestValues['flutter.bool']!); + await preferences.setValue('Int', key, kTestValues['flutter.int']!); + await preferences.setValue('Double', key, kTestValues['flutter.double']!); await preferences.setValue( - 'StringList', key, kTestValues['flutter.List']); + 'StringList', key, kTestValues['flutter.List']!); await preferences.remove(key); final Map values = await preferences.getAll(); expect(values[key], isNull); @@ -79,13 +81,13 @@ void main() { testWidgets('clearing', (WidgetTester _) async { await preferences.setValue( - 'String', 'String', kTestValues['flutter.String']); - await preferences.setValue('Bool', 'bool', kTestValues['flutter.bool']); - await preferences.setValue('Int', 'int', kTestValues['flutter.int']); + 'String', 'String', kTestValues['flutter.String']!); + await preferences.setValue('Bool', 'bool', kTestValues['flutter.bool']!); + await preferences.setValue('Int', 'int', kTestValues['flutter.int']!); await preferences.setValue( - 'Double', 'double', kTestValues['flutter.double']); + 'Double', 'double', kTestValues['flutter.double']!); await preferences.setValue( - 'StringList', 'List', kTestValues['flutter.List']); + 'StringList', 'List', kTestValues['flutter.List']!); await preferences.clear(); final Map values = await preferences.getAll(); expect(values['String'], null); diff --git a/packages/shared_preferences/shared_preferences_windows/example/lib/main.dart b/packages/shared_preferences/shared_preferences_windows/example/lib/main.dart index 0cdd37394706..423a727042cc 100644 --- a/packages/shared_preferences/shared_preferences_windows/example/lib/main.dart +++ b/packages/shared_preferences/shared_preferences_windows/example/lib/main.dart @@ -16,7 +16,7 @@ void main() { class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return MaterialApp( + return const MaterialApp( title: 'SharedPreferences Demo', home: SharedPreferencesDemo(), ); @@ -24,18 +24,18 @@ class MyApp extends StatelessWidget { } class SharedPreferencesDemo extends StatefulWidget { - SharedPreferencesDemo({Key? key}) : super(key: key); + const SharedPreferencesDemo({Key? key}) : super(key: key); @override SharedPreferencesDemoState createState() => SharedPreferencesDemoState(); } class SharedPreferencesDemoState extends State { - final prefs = SharedPreferencesWindows.instance; + final SharedPreferencesWindows prefs = SharedPreferencesWindows.instance; late Future _counter; Future _incrementCounter() async { - final values = await prefs.getAll(); + final Map values = await prefs.getAll(); final int counter = (values['counter'] as int? ?? 0) + 1; setState(() { @@ -49,7 +49,7 @@ class SharedPreferencesDemoState extends State { void initState() { super.initState(); _counter = prefs.getAll().then((Map values) { - return (values['counter'] as int? ?? 0); + return values['counter'] as int? ?? 0; }); } @@ -57,7 +57,7 @@ class SharedPreferencesDemoState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: const Text("SharedPreferences Demo"), + title: const Text('SharedPreferences Demo'), ), body: Center( child: FutureBuilder( 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 b8cd3702b837..02f9f85d6381 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,20 +4,21 @@ 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]. /// /// This class implements the `package:shared_preferences` functionality for Windows. class SharedPreferencesWindows extends SharedPreferencesStorePlatform { /// The default instance of [SharedPreferencesWindows] to use. - /// TODO(egarciad): Remove when the Dart plugin registrant lands on Flutter stable. - /// https://github.com/flutter/flutter/issues/81421 + // TODO(egarciad): Remove when the Dart plugin registrant lands on Flutter stable. + // https://github.com/flutter/flutter/issues/81421 static SharedPreferencesWindows instance = SharedPreferencesWindows(); /// Registers the Windows implementation. @@ -27,7 +28,7 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform { /// File system used to store to disk. Exposed for testing only. @visibleForTesting - FileSystem fs = LocalFileSystem(); + FileSystem fs = const LocalFileSystem(); /// The path_provider_windows instance used to find the support directory. @visibleForTesting @@ -44,7 +45,7 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform { if (_localDataFilePath != null) { return _localDataFilePath!; } - final directory = await pathProvider.getApplicationSupportPath(); + final String? directory = await pathProvider.getApplicationSupportPath(); if (directory == null) { return null; } @@ -58,12 +59,15 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform { if (_cachedPreferences != null) { return _cachedPreferences!; } - Map preferences = {}; + Map preferences = {}; final File? localDataFile = await _getLocalDataFile(); if (localDataFile != null && localDataFile.existsSync()) { - String stringMap = localDataFile.readAsStringSync(); + final String stringMap = localDataFile.readAsStringSync(); if (stringMap.isNotEmpty) { - preferences = json.decode(stringMap).cast(); + final Object? data = json.decode(stringMap); + if (data is Map) { + preferences = data.cast(); + } } } _cachedPreferences = preferences; @@ -76,16 +80,16 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform { try { final File? localDataFile = await _getLocalDataFile(); if (localDataFile == null) { - print("Unable to determine where to write preferences."); + print('Unable to determine where to write preferences.'); return false; } if (!localDataFile.existsSync()) { localDataFile.createSync(recursive: true); } - String stringMap = json.encode(preferences); + final String stringMap = json.encode(preferences); localDataFile.writeAsStringSync(stringMap); } catch (e) { - print("Error saving preferences to disk: $e"); + print('Error saving preferences to disk: $e'); return false; } return true; @@ -93,7 +97,7 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform { @override Future clear() async { - var preferences = await _readPreferences(); + final Map preferences = await _readPreferences(); preferences.clear(); return _writePreferences(preferences); } @@ -105,14 +109,14 @@ class SharedPreferencesWindows extends SharedPreferencesStorePlatform { @override Future remove(String key) async { - var preferences = await _readPreferences(); + final Map preferences = await _readPreferences(); preferences.remove(key); return _writePreferences(preferences); } @override Future setValue(String valueType, String key, Object value) async { - var preferences = await _readPreferences(); + final Map preferences = await _readPreferences(); preferences[key] = value; return _writePreferences(preferences); } diff --git a/packages/shared_preferences/shared_preferences_windows/pubspec.yaml b/packages/shared_preferences/shared_preferences_windows/pubspec.yaml index 87b685f6d0bc..76b3c74628b4 100644 --- a/packages/shared_preferences/shared_preferences_windows/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_windows/pubspec.yaml @@ -17,9 +17,9 @@ flutter: pluginClass: none dependencies: + file: ^6.0.0 flutter: sdk: flutter - file: ^6.0.0 meta: ^1.3.0 path: ^1.8.0 path_provider_platform_interface: ^2.0.0 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 6bb21b814e07..0c47e9865765 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 @@ -20,11 +20,11 @@ void main() { }); Future _getFilePath() async { - final directory = await pathProvider.getApplicationSupportPath(); + final String? directory = await pathProvider.getApplicationSupportPath(); return path.join(directory!, 'shared_preferences.json'); } - _writeTestFile(String value) async { + Future _writeTestFile(String value) async { fileSystem.file(await _getFilePath()) ..createSync(recursive: true) ..writeAsStringSync(value); @@ -35,7 +35,7 @@ void main() { } SharedPreferencesWindows _getPreferences() { - var prefs = SharedPreferencesWindows(); + final SharedPreferencesWindows prefs = SharedPreferencesWindows(); prefs.fs = fileSystem; prefs.pathProvider = pathProvider; return prefs; @@ -49,9 +49,9 @@ void main() { test('getAll', () async { await _writeTestFile('{"key1": "one", "key2": 2}'); - var prefs = _getPreferences(); + final SharedPreferencesWindows prefs = _getPreferences(); - var values = await prefs.getAll(); + final Map values = await prefs.getAll(); expect(values, hasLength(2)); expect(values['key1'], 'one'); expect(values['key2'], 2); @@ -59,7 +59,7 @@ void main() { test('remove', () async { await _writeTestFile('{"key1":"one","key2":2}'); - var prefs = _getPreferences(); + final SharedPreferencesWindows prefs = _getPreferences(); await prefs.remove('key2'); @@ -68,7 +68,7 @@ void main() { test('setValue', () async { await _writeTestFile('{}'); - var prefs = _getPreferences(); + final SharedPreferencesWindows prefs = _getPreferences(); await prefs.setValue('', 'key1', 'one'); await prefs.setValue('', 'key2', 2); @@ -78,7 +78,7 @@ void main() { test('clear', () async { await _writeTestFile('{"key1":"one","key2":2}'); - var prefs = _getPreferences(); + final SharedPreferencesWindows prefs = _getPreferences(); await prefs.clear(); expect(await _readTestFile(), '{}'); @@ -92,6 +92,7 @@ void main() { /// path it returns is a root path that does not actually exist on Windows. class FakePathProviderWindows extends PathProviderPlatform implements PathProviderWindows { + @override late VersionInfoQuerier versionInfoQuerier; @override diff --git a/script/configs/custom_analysis.yaml b/script/configs/custom_analysis.yaml index 71d8fb825add..3840d64f33f3 100644 --- a/script/configs/custom_analysis.yaml +++ b/script/configs/custom_analysis.yaml @@ -23,7 +23,6 @@ - ios_platform_images - local_auth - quick_actions -- shared_preferences - url_launcher - video_player - webview_flutter