Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 31a8b5c

Browse files
Migrate shared_preferences_platform_interfaces to null safety (#3466)
1 parent a16411b commit 31a8b5c

File tree

6 files changed

+33
-39
lines changed

6 files changed

+33
-39
lines changed

packages/shared_preferences/shared_preferences_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.0-nullsafety
2+
3+
* Migrate to null safety.
4+
15
## 1.0.5
26

37
* Update Flutter SDK constraint.

packages/shared_preferences/shared_preferences_platform_interface/lib/method_channel_shared_preferences.dart

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,32 @@ const MethodChannel _kChannel =
1818
class MethodChannelSharedPreferencesStore
1919
extends SharedPreferencesStorePlatform {
2020
@override
21-
Future<bool> remove(String key) {
22-
return _invokeBoolMethod('remove', <String, dynamic>{
23-
'key': key,
24-
});
21+
Future<bool> remove(String key) async {
22+
return (await _kChannel.invokeMethod<bool>(
23+
'remove',
24+
<String, dynamic>{'key': key},
25+
))!;
2526
}
2627

2728
@override
28-
Future<bool> setValue(String valueType, String key, Object value) {
29-
return _invokeBoolMethod('set$valueType', <String, dynamic>{
30-
'key': key,
31-
'value': value,
32-
});
33-
}
34-
35-
Future<bool> _invokeBoolMethod(String method, Map<String, dynamic> params) {
36-
return _kChannel
37-
.invokeMethod<bool>(method, params)
38-
// TODO(yjbanov): I copied this from the original
39-
// shared_preferences.dart implementation, but I
40-
// actually do not know why it's necessary to pipe the
41-
// result through an identity function.
42-
//
43-
// Source: https://github.com/flutter/plugins/blob/3a87296a40a2624d200917d58f036baa9fb18df8/packages/shared_preferences/lib/shared_preferences.dart#L134
44-
.then<bool>((dynamic result) => result);
29+
Future<bool> setValue(String valueType, String key, Object value) async {
30+
return (await _kChannel.invokeMethod<bool>(
31+
'set$valueType',
32+
<String, dynamic>{'key': key, 'value': value},
33+
))!;
4534
}
4635

4736
@override
48-
Future<bool> clear() {
49-
return _kChannel.invokeMethod<bool>('clear');
37+
Future<bool> clear() async {
38+
return (await _kChannel.invokeMethod<bool>('clear'))!;
5039
}
5140

5241
@override
53-
Future<Map<String, Object>> getAll() {
54-
return _kChannel.invokeMapMethod<String, Object>('getAll');
42+
Future<Map<String, Object>> getAll() async {
43+
final Map<String, Object>? preferences =
44+
await _kChannel.invokeMapMethod<String, Object>('getAll');
45+
46+
if (preferences == null) return <String, Object>{};
47+
return preferences;
5548
}
5649
}

packages/shared_preferences/shared_preferences_platform_interface/lib/shared_preferences_platform_interface.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'dart:async';
66

7-
import 'package:meta/meta.dart';
7+
import 'package:flutter/foundation.dart';
88

99
import 'method_channel_shared_preferences.dart';
1010

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
name: shared_preferences_platform_interface
22
description: A common platform interface for the shared_preferences plugin.
33
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences_platform_interface
4-
version: 1.0.5
4+
version: 2.0.0-nullsafety
55

66
dependencies:
7-
meta: ^1.0.4
87
flutter:
98
sdk: flutter
109

1110
dev_dependencies:
1211
flutter_test:
1312
sdk: flutter
14-
pedantic: ^1.8.0
13+
pedantic: ^1.10.0-nullsafety
1514

1615
environment:
17-
sdk: ">=2.1.0 <3.0.0"
16+
sdk: ">=2.12.0-0 <3.0.0"
1817
flutter: ">=1.12.8"

packages/shared_preferences/shared_preferences_platform_interface/test/method_channel_shared_preferences_test.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ void main() {
1515
'plugins.flutter.io/shared_preferences',
1616
);
1717

18-
const Map<String, dynamic> kTestValues = <String, dynamic>{
18+
const Map<String, Object> kTestValues = <String, Object>{
1919
'flutter.String': 'hello world',
2020
'flutter.Bool': true,
2121
'flutter.Int': 42,
2222
'flutter.Double': 3.14159,
2323
'flutter.StringList': <String>['foo', 'bar'],
2424
};
2525

26-
InMemorySharedPreferencesStore testData;
26+
late InMemorySharedPreferencesStore testData;
2727

2828
final List<MethodCall> log = <MethodCall>[];
29-
MethodChannelSharedPreferencesStore store;
29+
late MethodChannelSharedPreferencesStore store;
3030

3131
setUp(() async {
3232
testData = InMemorySharedPreferencesStore.empty();
@@ -44,9 +44,9 @@ void main() {
4444
return await testData.clear();
4545
}
4646
final RegExp setterRegExp = RegExp(r'set(.*)');
47-
final Match match = setterRegExp.matchAsPrefix(methodCall.method);
48-
if (match.groupCount == 1) {
49-
final String valueType = match.group(1);
47+
final Match? match = setterRegExp.matchAsPrefix(methodCall.method);
48+
if (match?.groupCount == 1) {
49+
final String valueType = match!.group(1)!;
5050
final String key = methodCall.arguments['key'];
5151
final Object value = methodCall.arguments['value'];
5252
return await testData.setValue(valueType, key, value);
@@ -59,8 +59,6 @@ void main() {
5959

6060
tearDown(() async {
6161
await testData.clear();
62-
store = null;
63-
testData = null;
6462
});
6563

6664
test('getAll', () async {

script/nnbd_plugins.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ readonly NNBD_PLUGINS_LIST=(
1717
"path_provider"
1818
"plugin_platform_interface"
1919
"share"
20+
"shared_preferences"
2021
"url_launcher"
2122
"video_player"
2223
"webview_flutter"
@@ -35,7 +36,6 @@ readonly NON_NNBD_PLUGINS_LIST=(
3536
# "in_app_purchase"
3637
# "quick_actions"
3738
# "sensors"
38-
# "shared_preferences"
3939
# "wifi_info_flutter"
4040
)
4141

0 commit comments

Comments
 (0)