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

Migrate shared_preferences_platform_interfaces to null safety #3466

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.0-nullsafety

* Migrate to null safety.

## 1.0.5

* Update Flutter SDK constraint.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,32 @@ const MethodChannel _kChannel =
class MethodChannelSharedPreferencesStore
extends SharedPreferencesStorePlatform {
@override
Future<bool> remove(String key) {
return _invokeBoolMethod('remove', <String, dynamic>{
'key': key,
});
Future<bool> remove(String key) async {
return (await _kChannel.invokeMethod<bool>(
'remove',
<String, dynamic>{'key': key},
))!;
}

@override
Future<bool> setValue(String valueType, String key, Object value) {
return _invokeBoolMethod('set$valueType', <String, dynamic>{
'key': key,
'value': value,
});
}

Future<bool> _invokeBoolMethod(String method, Map<String, dynamic> params) {
return _kChannel
.invokeMethod<bool>(method, params)
// TODO(yjbanov): I copied this from the original
// shared_preferences.dart implementation, but I
// actually do not know why it's necessary to pipe the
// result through an identity function.
//
// Source: https://github.com/flutter/plugins/blob/3a87296a40a2624d200917d58f036baa9fb18df8/packages/shared_preferences/lib/shared_preferences.dart#L134
.then<bool>((dynamic result) => result);
Future<bool> setValue(String valueType, String key, Object value) async {
return (await _kChannel.invokeMethod<bool>(
'set$valueType',
<String, dynamic>{'key': key, 'value': value},
))!;
}

@override
Future<bool> clear() {
return _kChannel.invokeMethod<bool>('clear');
Future<bool> clear() async {
return (await _kChannel.invokeMethod<bool>('clear'))!;
}

@override
Future<Map<String, Object>> getAll() {
return _kChannel.invokeMapMethod<String, Object>('getAll');
Future<Map<String, Object>> getAll() async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we certain that no platform implementation ever returns null here? It might be a good idea to bullet-proof this with a null check on the results, returning an empty map if the channel's result was null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

final Map<String, Object>? preferences =
await _kChannel.invokeMapMethod<String, Object>('getAll');

if (preferences == null) return <String, Object>{};
return preferences;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import 'dart:async';

import 'package:meta/meta.dart';
import 'package:flutter/foundation.dart';

import 'method_channel_shared_preferences.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
name: shared_preferences_platform_interface
description: A common platform interface for the shared_preferences plugin.
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences_platform_interface
version: 1.0.5
version: 2.0.0-nullsafety

dependencies:
meta: ^1.0.4
flutter:
sdk: flutter

dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.8.0
pedantic: ^1.10.0-nullsafety

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.12.0-0 <3.0.0"
flutter: ">=1.12.8"
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ void main() {
'plugins.flutter.io/shared_preferences',
);

const Map<String, dynamic> kTestValues = <String, dynamic>{
const Map<String, Object> kTestValues = <String, Object>{
'flutter.String': 'hello world',
'flutter.Bool': true,
'flutter.Int': 42,
'flutter.Double': 3.14159,
'flutter.StringList': <String>['foo', 'bar'],
};

InMemorySharedPreferencesStore testData;
late InMemorySharedPreferencesStore testData;

final List<MethodCall> log = <MethodCall>[];
MethodChannelSharedPreferencesStore store;
late MethodChannelSharedPreferencesStore store;

setUp(() async {
testData = InMemorySharedPreferencesStore.empty();
Expand All @@ -44,9 +44,9 @@ void main() {
return await testData.clear();
}
final RegExp setterRegExp = RegExp(r'set(.*)');
final Match match = setterRegExp.matchAsPrefix(methodCall.method);
if (match.groupCount == 1) {
final String valueType = match.group(1);
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'];
return await testData.setValue(valueType, key, value);
Expand All @@ -59,8 +59,6 @@ void main() {

tearDown(() async {
await testData.clear();
store = null;
testData = null;
});

test('getAll', () async {
Expand Down
2 changes: 1 addition & 1 deletion script/nnbd_plugins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ readonly NNBD_PLUGINS_LIST=(
"path_provider"
"plugin_platform_interface"
"share"
"shared_preferences"
"url_launcher"
"video_player"
"webview_flutter"
Expand All @@ -34,7 +35,6 @@ readonly NON_NNBD_PLUGINS_LIST=(
# "in_app_purchase"
# "quick_actions"
# "sensors"
# "shared_preferences"
# "wifi_info_flutter"
)

Expand Down