Storage class
The type used by the Window.localStorage and Window.sessionStorage properties. Storage is implemented as a Map<String, String>.
To store and get values, use Dart's built-in map syntax:
window.localStorage['key1'] = 'val1';
window.localStorage['key2'] = 'val2';
window.localStorage['key3'] = 'val3';
assert(window.localStorage['key3'] == 'val3');
You can use Map APIs such as containsValue(), clear(), and length:
assert(window.localStorage.containsValue('does not exist') == false);
window.localStorage.clear();
assert(window.localStorage.length == 0);
For more examples of using this API, see localstorage_test.dart. For details on using the Map API, see the Maps section of the library tour.
@DomName('Storage')
class Storage implements Map<String, String>
native "Storage" {
// TODO(nweiz): update this when maps support lazy iteration
bool containsValue(String value) => values.any((e) => e == value);
bool containsKey(String key) => $dom_getItem(key) != null;
String operator [](String key) => $dom_getItem(key);
void operator []=(String key, String value) { $dom_setItem(key, value); }
String putIfAbsent(String key, String ifAbsent()) {
if (!containsKey(key)) this[key] = ifAbsent();
return this[key];
}
String remove(String key) {
final value = this[key];
$dom_removeItem(key);
return value;
}
void clear() => $dom_clear();
void forEach(void f(String key, String value)) {
for (var i = 0; true; i++) {
final key = $dom_key(i);
if (key == null) return;
f(key, this[key]);
}
}
Iterable<String> get keys {
final keys = [];
forEach((k, v) => keys.add(k));
return keys;
}
Iterable<String> get values {
final values = [];
forEach((k, v) => values.add(v));
return values;
}
int get length => $dom_length;
bool get isEmpty => $dom_key(0) == null;
@JSName('length')
@DomName('Storage.length')
@DocsEditable
final int $dom_length;
@JSName('clear')
@DomName('Storage.clear')
@DocsEditable
void $dom_clear() native;
@JSName('getItem')
@DomName('Storage.getItem')
@DocsEditable
String $dom_getItem(String key) native;
@JSName('key')
@DomName('Storage.key')
@DocsEditable
String $dom_key(int index) native;
@JSName('removeItem')
@DomName('Storage.removeItem')
@DocsEditable
void $dom_removeItem(String key) native;
@JSName('setItem')
@DomName('Storage.setItem')
@DocsEditable
void $dom_setItem(String key, String data) native;
}
Extends
Interceptor > Storage
Implements
Properties
final int hashCode #
Get a hash code for this object.
All objects have hash codes. Hash codes are guaranteed to be the
same for objects that are equal when compared using the equality
operator ==. Other than that there are no guarantees about
the hash codes. They will not be consistent between runs and
there are no distribution guarantees.
If a subclass overrides hashCode it should override the
equality operator as well to maintain consistency.
int get hashCode => Primitives.objectHashCode(this);
Operators
String operator [](String key) #
Returns the value for the given key or null if key is not in the map. Because null values are supported, one should either use containsKey to distinguish between an absent key and a null value, or use the putIfAbsent method.
String operator [](String key) => $dom_getItem(key);
bool operator ==(other) #
The equality operator.
The default behavior for all Objects is to return true if and
only if this and
other are the same object.
If a subclass overrides the equality operator it should override
the hashCode method as well to maintain consistency.
bool operator ==(other) => identical(this, other);
Methods
void clear() #
void forEach(void f(String key, String value)) #
Applies f to each {key, value} pair of the map.
It is an error to add or remove keys from the map during iteration.
void forEach(void f(String key, String value)) {
for (var i = 0; true; i++) {
final key = $dom_key(i);
if (key == null) return;
f(key, this[key]);
}
}
dynamic noSuchMethod(Invocation invocation) #
noSuchMethod is invoked when users invoke a non-existant method
on an object. The name of the method and the arguments of the
invocation are passed to noSuchMethod in an Invocation.
If noSuchMethod returns a value, that value becomes the result of
the original invocation.
The default behavior of noSuchMethod is to throw a
noSuchMethodError.
dynamic noSuchMethod(Invocation invocation) {
throw new NoSuchMethodError(
this,
_symbolToString(invocation.memberName),
invocation.positionalArguments,
_symbolMapToStringMap(invocation.namedArguments));
}
String putIfAbsent(String key, String ifAbsent()) #
If key is not associated to a value, calls ifAbsent and updates the map by mapping key to the value returned by ifAbsent. Returns the value in the map.
It is an error to add or remove keys from map during the call to ifAbsent.
String putIfAbsent(String key, String ifAbsent()) {
if (!containsKey(key)) this[key] = ifAbsent();
return this[key];
}
String remove(String key) #
Removes the association for the given key. Returns the value for key in the map or null if key is not in the map. Note that values can be null and a returned null value does not always imply that the key is absent.
String remove(String key) {
final value = this[key];
$dom_removeItem(key);
return value;
}
String toString() #
Returns a string representation of this object.
String toString() => Primitives.objectToString(this);
void $dom_clear() #
@JSName('clear')
@DomName('Storage.clear')
@DocsEditable
void $dom_clear() native;
String $dom_getItem(String key) #
@JSName('getItem')
@DomName('Storage.getItem')
@DocsEditable
String $dom_getItem(String key) native;
String $dom_key(int index) #
@JSName('key')
@DomName('Storage.key')
@DocsEditable
String $dom_key(int index) native;