Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
Next Next commit
wip
  • Loading branch information
kevmoo committed Oct 12, 2023
commit adafc76bc29fe0284f2cfdecfc5cedc5f81295ea
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 @@ -42,8 +42,11 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform {
// IMPORTANT: Do not use html.window.localStorage.clear() as that will
// remove _all_ local data, not just the keys prefixed with
// _prefix
_getFilteredKeys(filter.prefix, allowList: filter.allowList)
.forEach(html.window.localStorage.remove);
// ignore: prefer_foreach
for (final String item
in _getFilteredKeys(filter.prefix, allowList: filter.allowList)) {
html.window.localStorage.removeItem(item);
}
return true;
}

Expand All @@ -69,29 +72,33 @@ 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;
}

Iterable<String> _getFilteredKeys(
String prefix, {
Set<String>? allowList,
}) {
return html.window.localStorage.keys.where((String key) =>
key.startsWith(prefix) && (allowList?.contains(key) ?? true));
}) sync* {
for (int i = 0; i < html.window.localStorage.length; i++) {
final String key = html.window.localStorage.key(i)!;
if (key.startsWith(prefix) && (allowList?.contains(key) ?? true)) {
yield key;
}
}
}

String _encodeValue(Object? value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies:
flutter_web_plugins:
sdk: flutter
shared_preferences_platform_interface: ^2.3.0
web: ^0.1.4-beta

dev_dependencies:
flutter_test:
Expand Down