Dart API Referencedart:htmlStorage

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

Map<String, String>

Properties

final int $dom_length #

final int $dom_length

final int hashCode #

inherited from Interceptor

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.

docs inherited from Object
int get hashCode => Primitives.objectHashCode(this);

final bool isEmpty #

Returns true if there is no {key, value} pair in the map.

docs inherited from Map<String, String>
bool get isEmpty => $dom_key(0) == null;

final Iterable<String> keys #

The keys of this.

docs inherited from Map<String, String>
Iterable<String> get keys {
 final keys = [];
 forEach((k, v) => keys.add(k));
 return keys;
}

final int length #

The number of {key, value} pairs in the map.

docs inherited from Map<String, String>
int get length => $dom_length;

final Type runtimeType #

inherited from Interceptor

A representation of the runtime type of the object.

docs inherited from Object
Type get runtimeType => getRuntimeType(this);

final Iterable<String> values #

The values of this.

docs inherited from Map<String, String>
Iterable<String> get values {
 final values = [];
 forEach((k, v) => values.add(v));
 return values;
}

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.

docs inherited from Map<String, String>
String operator [](String key) => $dom_getItem(key);

void operator []=(String key, String value) #

Associates the key with the given value.

docs inherited from Map<String, String>
void operator []=(String key, String value) { $dom_setItem(key, value); }

bool operator ==(other) #

inherited from Interceptor

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.

docs inherited from Object
bool operator ==(other) => identical(this, other);

Methods

void clear() #

Removes all pairs from the map.

docs inherited from Map<String, String>
void clear() => $dom_clear();

bool containsKey(String key) #

Returns whether this map contains the given key.

docs inherited from Map<String, String>
bool containsKey(String key) => $dom_getItem(key) != null;

bool containsValue(String value) #

Returns whether this map contains the given value.

docs inherited from Map<String, String>
bool containsValue(String value) => values.any((e) => e == value);

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.

docs inherited from Map<String, String>
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) #

inherited from Interceptor

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.

docs inherited from Object
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.

docs inherited from Map<String, String>
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.

docs inherited from Map<String, String>
String remove(String key) {
 final value = this[key];
 $dom_removeItem(key);
 return value;
}

String toString() #

inherited from Interceptor

Returns a string representation of this object.

docs inherited from 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;

void $dom_removeItem(String key) #

@JSName('removeItem')
@DomName('Storage.removeItem')
@DocsEditable
void $dom_removeItem(String key) native;

void $dom_setItem(String key, String data) #

@JSName('setItem')
@DomName('Storage.setItem')
@DocsEditable
void $dom_setItem(String key, String data) native;