4
4
5
5
import 'dart:async' ;
6
6
import 'dart:io' ;
7
+
7
8
import 'package:flutter/services.dart' ;
8
9
import 'package:flutter_test/flutter_test.dart' ;
9
- import 'package:shared_preferences/shared_preferences.dart' ;
10
10
import 'package:integration_test/integration_test.dart' ;
11
+ import 'package:shared_preferences/shared_preferences.dart' ;
11
12
12
13
void main () {
13
14
IntegrationTestWidgetsFlutterBinding .ensureInitialized ();
14
15
15
16
group ('$SharedPreferences ' , () {
16
- const Map <String , dynamic > kTestValues = < String , dynamic > {
17
- 'flutter.String' : 'hello world' ,
18
- 'flutter.bool' : true ,
19
- 'flutter.int' : 42 ,
20
- 'flutter.double' : 3.14159 ,
21
- 'flutter.List' : < String > ['foo' , 'bar' ],
22
- };
17
+ const String testString = 'hello world' ;
18
+ const bool testBool = true ;
19
+ const int testInt = 42 ;
20
+ const double testDouble = 3.14159 ;
21
+ const List <String > testList = < String > ['foo' , 'bar' ];
23
22
24
- const Map <String , dynamic > kTestValues2 = < String , dynamic > {
25
- 'flutter.String' : 'goodbye world' ,
26
- 'flutter.bool' : false ,
27
- 'flutter.int' : 1337 ,
28
- 'flutter.double' : 2.71828 ,
29
- 'flutter.List' : < String > ['baz' , 'quox' ],
30
- };
23
+ const String testString2 = 'goodbye world' ;
24
+ const bool testBool2 = false ;
25
+ const int testInt2 = 1337 ;
26
+ const double testDouble2 = 2.71828 ;
27
+ const List <String > testList2 = < String > ['baz' , 'quox' ];
31
28
32
29
late SharedPreferences preferences;
33
30
@@ -54,36 +51,36 @@ void main() {
54
51
55
52
testWidgets ('writing' , (WidgetTester _) async {
56
53
await Future .wait (< Future <bool >> [
57
- preferences.setString ('String' , kTestValues2[ 'flutter.String' ] ),
58
- preferences.setBool ('bool' , kTestValues2[ 'flutter.bool' ] ),
59
- preferences.setInt ('int' , kTestValues2[ 'flutter.int' ] ),
60
- preferences.setDouble ('double' , kTestValues2[ 'flutter.double' ] ),
61
- preferences.setStringList ('List' , kTestValues2[ 'flutter.List' ] )
54
+ preferences.setString ('String' , testString2 ),
55
+ preferences.setBool ('bool' , testBool2 ),
56
+ preferences.setInt ('int' , testInt2 ),
57
+ preferences.setDouble ('double' , testDouble2 ),
58
+ preferences.setStringList ('List' , testList2 )
62
59
]);
63
- expect (preferences.getString ('String' ), kTestValues2[ 'flutter.String' ] );
64
- expect (preferences.getBool ('bool' ), kTestValues2[ 'flutter.bool' ] );
65
- expect (preferences.getInt ('int' ), kTestValues2[ 'flutter.int' ] );
66
- expect (preferences.getDouble ('double' ), kTestValues2[ 'flutter.double' ] );
67
- expect (preferences.getStringList ('List' ), kTestValues2[ 'flutter.List' ] );
60
+ expect (preferences.getString ('String' ), testString2 );
61
+ expect (preferences.getBool ('bool' ), testBool2 );
62
+ expect (preferences.getInt ('int' ), testInt2 );
63
+ expect (preferences.getDouble ('double' ), testDouble2 );
64
+ expect (preferences.getStringList ('List' ), testList2 );
68
65
});
69
66
70
67
testWidgets ('removing' , (WidgetTester _) async {
71
68
const String key = 'testKey' ;
72
- await preferences.setString (key, kTestValues[ 'flutter.String' ] );
73
- await preferences.setBool (key, kTestValues[ 'flutter.bool' ] );
74
- await preferences.setInt (key, kTestValues[ 'flutter.int' ] );
75
- await preferences.setDouble (key, kTestValues[ 'flutter.double' ] );
76
- await preferences.setStringList (key, kTestValues[ 'flutter.List' ] );
69
+ await preferences.setString (key, testString );
70
+ await preferences.setBool (key, testBool );
71
+ await preferences.setInt (key, testInt );
72
+ await preferences.setDouble (key, testDouble );
73
+ await preferences.setStringList (key, testList );
77
74
await preferences.remove (key);
78
75
expect (preferences.get ('testKey' ), isNull);
79
76
});
80
77
81
78
testWidgets ('clearing' , (WidgetTester _) async {
82
- await preferences.setString ('String' , kTestValues[ 'flutter.String' ] );
83
- await preferences.setBool ('bool' , kTestValues[ 'flutter.bool' ] );
84
- await preferences.setInt ('int' , kTestValues[ 'flutter.int' ] );
85
- await preferences.setDouble ('double' , kTestValues[ 'flutter.double' ] );
86
- await preferences.setStringList ('List' , kTestValues[ 'flutter.List' ] );
79
+ await preferences.setString ('String' , testString );
80
+ await preferences.setBool ('bool' , testBool );
81
+ await preferences.setInt ('int' , testInt );
82
+ await preferences.setDouble ('double' , testDouble );
83
+ await preferences.setStringList ('List' , testList );
87
84
await preferences.clear ();
88
85
expect (preferences.getString ('String' ), null );
89
86
expect (preferences.getBool ('bool' ), null );
@@ -94,13 +91,13 @@ void main() {
94
91
95
92
testWidgets ('simultaneous writes' , (WidgetTester _) async {
96
93
final List <Future <bool >> writes = < Future <bool >> [];
97
- final int writeCount = 100 ;
94
+ const int writeCount = 100 ;
98
95
for (int i = 1 ; i <= writeCount; i++ ) {
99
96
writes.add (preferences.setInt ('int' , i));
100
97
}
101
- List <bool > result = await Future .wait (writes, eagerError: true );
98
+ final List <bool > result = await Future .wait (writes, eagerError: true );
102
99
// All writes should succeed.
103
- expect (result.where ((element) => ! element), isEmpty);
100
+ expect (result.where ((bool element) => ! element), isEmpty);
104
101
// The last write should win.
105
102
expect (preferences.getInt ('int' ), writeCount);
106
103
});
@@ -112,28 +109,22 @@ void main() {
112
109
// special prefixes plus a string value
113
110
expect (
114
111
// prefix for lists
115
- preferences.setString (
116
- 'String' ,
117
- 'VGhpcyBpcyB0aGUgcHJlZml4IGZvciBhIGxpc3Qu' +
118
- kTestValues2['flutter.String' ]),
112
+ preferences.setString ('String' ,
113
+ 'VGhpcyBpcyB0aGUgcHJlZml4IGZvciBhIGxpc3Qu' + testString),
119
114
throwsA (isA <PlatformException >()));
120
115
await preferences.reload ();
121
116
expect (preferences.getString ('String' ), null );
122
117
expect (
123
118
// prefix for big integers
124
- preferences.setString (
125
- 'String' ,
126
- 'VGhpcyBpcyB0aGUgcHJlZml4IGZvciBCaWdJbnRlZ2Vy' +
127
- kTestValues2['flutter.String' ]),
119
+ preferences.setString ('String' ,
120
+ 'VGhpcyBpcyB0aGUgcHJlZml4IGZvciBCaWdJbnRlZ2Vy' + testString),
128
121
throwsA (isA <PlatformException >()));
129
122
await preferences.reload ();
130
123
expect (preferences.getString ('String' ), null );
131
124
expect (
132
125
// prefix for doubles
133
- preferences.setString (
134
- 'String' ,
135
- 'VGhpcyBpcyB0aGUgcHJlZml4IGZvciBEb3VibGUu' +
136
- kTestValues2['flutter.String' ]),
126
+ preferences.setString ('String' ,
127
+ 'VGhpcyBpcyB0aGUgcHJlZml4IGZvciBEb3VibGUu' + testString),
137
128
throwsA (isA <PlatformException >()));
138
129
await preferences.reload ();
139
130
expect (preferences.getString ('String' ), null );
0 commit comments