Shared Preferences Web - Dart
Shared Preferences Web - Dart
Shared Preferences Web - Dart
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_platfor
m_interface.dart';
@override
Future<bool> clear() async {
// IMPORTANT: Do not use html.window.localStorage.clear() as that will
// remove _all_ local data, not just the keys prefixed with
// "flutter."
for (String key in _storedFlutterKeys) {
html.window.localStorage.remove(key);
}
return true;
}
@override
Future<Map<String, Object>> getAll() async {
final Map<String, Object> allData = <String, Object>{};
for (String key in _storedFlutterKeys) {
allData[key] = _decodeValue(html.window.localStorage[key]);
}
return allData;
}
@override
Future<bool> remove(String key) async {
_checkPrefix(key);
html.window.localStorage.remove(key);
return true;
}
@override
Future<bool> setValue(String valueType, String key, Object value) async {
_checkPrefix(key);
html.window.localStorage[key] = _encodeValue(value);
return true;
}
void _checkPrefix(String key) {
if (!key.startsWith('flutter.')) {
throw FormatException(
'Shared preferences keys must start with prefix "flutter.".',
key,
0,
);
}
}
if (decodedValue is List) {
// JSON does not preserve generics. The encode/decode roundtrip is
// `List<String>` => JSON => `List<dynamic>`. We have to explicitly
// restore the RTTI.
return decodedValue.cast<String>();
}
return decodedValue;
}
}