Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[shared_preferences_web] migrate to pkg:web #5414

Merged
merged 4 commits into from
Nov 18, 2023
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,6 +1,6 @@
## NEXT
## 2.2.2

* Updates minimum supported SDK version to Flutter 3.10/Dart 3.0.
* Updates minimum supported SDK version to Dart 3.2.

## 2.2.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:html' as html;

import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:shared_preferences_platform_interface/method_channel_shared_preferences.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
import 'package:shared_preferences_platform_interface/types.dart';
import 'package:shared_preferences_web/shared_preferences_web.dart';
import 'package:shared_preferences_web/src/keys_extension.dart';

import 'package:web/helpers.dart' as html;

const Map<String, dynamic> kTestValues = <String, dynamic>{
'flutter.String': 'hello world',
Expand Down Expand Up @@ -114,6 +115,15 @@ void main() {
allTestValues['flutter.StringList']!);
}

testWidgets('keys', (WidgetTester _) async {
await addData();
final Iterable<String> keys = html.window.localStorage.keys;
final Iterable<String> expectedKeys = allTestValues.keys;

expect(keys, hasLength(expectedKeys.length));
expect(keys, containsAll(expectedKeys));
});

testWidgets('clear', (WidgetTester _) async {
await addData();
await preferences.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ name: shared_preferences_web_integration_tests
publish_to: none

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.10.0"
sdk: ">=3.2.0 <4.0.0"
flutter: ">=3.16.0"

dependencies:
flutter:
sdk: flutter
shared_preferences_platform_interface: ^2.3.0
shared_preferences_web:
path: ../
web: '>=0.3.0 <0.5.0'

dev_dependencies:
flutter_test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

import 'dart:async';
import 'dart:convert' show json;
import 'dart:html' as html;

import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
import 'package:shared_preferences_platform_interface/types.dart';
import 'package:web/web.dart' as html;

import 'src/keys_extension.dart';

/// The web implementation of [SharedPreferencesStorePlatform].
///
Expand Down Expand Up @@ -43,7 +45,7 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform {
// remove _all_ local data, not just the keys prefixed with
// _prefix
_getFilteredKeys(filter.prefix, allowList: filter.allowList)
.forEach(html.window.localStorage.remove);
.forEach(remove);
return true;
}

Expand All @@ -69,20 +71,20 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform {
final Map<String, Object> allData = <String, Object>{};
for (final String key
in _getFilteredKeys(filter.prefix, allowList: filter.allowList)) {
allData[key] = _decodeValue(html.window.localStorage[key]!);
allData[key] = _decodeValue(html.window.localStorage.getItem(key)!);
}
return allData;
}

@override
Future<bool> remove(String key) async {
html.window.localStorage.remove(key);
html.window.localStorage.removeItem(key);
return true;
}

@override
Future<bool> setValue(String valueType, String key, Object? value) async {
html.window.localStorage[key] = _encodeValue(value);
html.window.localStorage.setItem(key, _encodeValue(value));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:web/web.dart' as html;

/// An extension on [html.Storage] that adds a convenience [keys] getter.
extension KeysExtension on html.Storage {
/// Gets all the [keys] set in this [html.Storage].
List<String> get keys {
return <String>[for (int i = 0; i < length; i++) key(i)!];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ name: shared_preferences_web
description: Web platform implementation of shared_preferences
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.2.1
version: 2.2.2

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.10.0"
sdk: ">=3.2.0 <4.0.0"
flutter: ">=3.16.0"

flutter:
plugin:
Expand All @@ -22,6 +22,7 @@ dependencies:
flutter_web_plugins:
sdk: flutter
shared_preferences_platform_interface: ^2.3.0
web: '>=0.3.0 <0.5.0'

dev_dependencies:
flutter_test:
Expand Down