0% found this document useful (0 votes)
125 views2 pages

Shared Preferences Web - Dart

The document describes a web implementation of the SharedPreferencesStorePlatform class that uses browser localStorage to persist key-value data. It implements methods like clear, getAll, remove and setValue to work with the localStorage API, encoding and decoding values as JSON. On web, it provides the shared preferences functionality for Flutter apps.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views2 pages

Shared Preferences Web - Dart

The document describes a web implementation of the SharedPreferencesStorePlatform class that uses browser localStorage to persist key-value data. It implements methods like clear, getAll, remove and setValue to work with the localStorage API, encoding and decoding values as JSON. On web, it provides the shared preferences functionality for Flutter apps.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

// Copyright 2017 The Chromium 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 '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';

/// The web implementation of [SharedPreferencesStorePlatform].


///
/// This class implements the `package:shared_preferences` functionality for the
web.
class SharedPreferencesPlugin extends SharedPreferencesStorePlatform {
/// Registers this class as the default instance of [SharedPreferencesStorePla
tform].
static void registerWith(Registrar registrar) {
SharedPreferencesStorePlatform.instance = SharedPreferencesPlugin();
}

@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,
);
}
}

List<String> get _storedFlutterKeys {


final List<String> keys = <String>[];
for (String key in html.window.localStorage.keys) {
if (key.startsWith('flutter.')) {
keys.add(key);
}
}
return keys;
}

String _encodeValue(Object value) {


return json.encode(value);
}

Object _decodeValue(String encodedValue) {


final Object decodedValue = json.decode(encodedValue);

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;
}
}

You might also like