Skip to content

Commit

Permalink
[shared_preferences] Switch to new analysis options (flutter#4384)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmorgan authored and KyleFin committed Dec 21, 2021
1 parent 6ccfc9c commit bfdc06e
Show file tree
Hide file tree
Showing 31 changed files with 296 additions and 257 deletions.
1 change: 0 additions & 1 deletion packages/shared_preferences/analysis_options.yaml

This file was deleted.

4 changes: 4 additions & 0 deletions packages/shared_preferences/shared_preferences/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> kTestValues = <String, dynamic>{
'flutter.String': 'hello world',
'flutter.bool': true,
'flutter.int': 42,
'flutter.double': 3.14159,
'flutter.List': <String>['foo', 'bar'],
};
const String testString = 'hello world';
const bool testBool = true;
const int testInt = 42;
const double testDouble = 3.14159;
const List<String> testList = <String>['foo', 'bar'];

const Map<String, dynamic> kTestValues2 = <String, dynamic>{
'flutter.String': 'goodbye world',
'flutter.bool': false,
'flutter.int': 1337,
'flutter.double': 2.71828,
'flutter.List': <String>['baz', 'quox'],
};
const String testString2 = 'goodbye world';
const bool testBool2 = false;
const int testInt2 = 1337;
const double testDouble2 = 2.71828;
const List<String> testList2 = <String>['baz', 'quox'];

late SharedPreferences preferences;

Expand All @@ -54,36 +51,36 @@ void main() {

testWidgets('writing', (WidgetTester _) async {
await Future.wait(<Future<bool>>[
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);
Expand All @@ -94,13 +91,13 @@ void main() {

testWidgets('simultaneous writes', (WidgetTester _) async {
final List<Future<bool>> writes = <Future<bool>>[];
final int writeCount = 100;
const int writeCount = 100;
for (int i = 1; i <= writeCount; i++) {
writes.add(preferences.setInt('int', i));
}
List<bool> result = await Future.wait(writes, eagerError: true);
final List<bool> 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);
});
Expand All @@ -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<PlatformException>()));
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<PlatformException>()));
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<PlatformException>()));
await preferences.reload();
expect(preferences.getString('String'), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ void main() {
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
return const MaterialApp(
title: 'SharedPreferences Demo',
home: SharedPreferencesDemo(),
);
}
}

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<SharedPreferencesDemo> {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
late Future<int> _counter;

Future<void> _incrementCounter() async {
final SharedPreferences prefs = await _prefs;
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;
});
});
Expand All @@ -49,15 +49,15 @@ class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
void initState() {
super.initState();
_counter = _prefs.then((SharedPreferences prefs) {
return (prefs.getInt('counter') ?? 0);
return prefs.getInt('counter') ?? 0;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("SharedPreferences Demo"),
title: const Text('SharedPreferences Demo'),
),
body: Center(
child: FutureBuilder<int>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class SharedPreferences {
/// performance-sensitive blocks.
static Future<SharedPreferences> getInstance() async {
if (_completer == null) {
final completer = Completer<SharedPreferences>();
final Completer<SharedPreferences> completer =
Completer<SharedPreferences>();
try {
final Map<String, Object> preferencesMap =
await _getSharedPreferencesMap();
Expand Down Expand Up @@ -188,7 +189,7 @@ class SharedPreferences {
assert(fromSystem != null);
// Strip the flutter. prefix from the returned preferences.
final Map<String, Object> preferencesMap = <String, Object>{};
for (String key in fromSystem.keys) {
for (final String key in fromSystem.keys) {
assert(key.startsWith(_prefix));
preferencesMap[key.substring(_prefix.length)] = fromSystem[key]!;
}
Expand Down
Loading

0 comments on commit bfdc06e

Please sign in to comment.