Skip to content

Commit

Permalink
[shared_preferences_web] migrate to pkg:web
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed Nov 17, 2023
1 parent a8bb833 commit 31528d2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.2

* Updates minimum supported SDK version to Dart 3.2.

## 2.2.1

* Adds pub topics to package metadata.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

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;

/// The web implementation of [SharedPreferencesStorePlatform].
///
Expand Down Expand Up @@ -69,20 +69,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 Expand Up @@ -111,3 +111,12 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform {
return decodedValue!;
}
}

extension on html.Storage {
void remove(String item) => removeItem(item);
Iterable<String> get keys sync* {
for (int i = 0; i < html.window.localStorage.length; i++) {
yield html.window.localStorage.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: ">=2.19.0 <4.0.0"
flutter: ">=3.7.0"
sdk: ^3.2.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

0 comments on commit 31528d2

Please sign in to comment.