AnyValueMap

Cross-language implementation of a dynamic map (dictionary) object that can hold values of any type. It also provides several methods to convert the stored values to different types.

Extends: MapBase<dynamic, dynamic>

Implements: ICloneable, IValueWrapper

Description

The AnyValueMap class provides a cross-language implementation of a dynamic map (dictionary) object that can hold values of any type. In addition, it provides several methods to convert the stored values to different types, such as boolean, integer or datetime.

Constructors

Creates a new instance of the map and assigns its value.

AnyValueMap([values])

  • values: dynamic - (optional) values to initialize this map.

Instance methods

append

Appends new elements to this map.

void append(map)

  • map: dynamic - map with elements to be added.

clear

Clears this map by removing all its elements.

@override

void clear()

clone

Creates a binary clone of this object.

@override

dynamic clone()

  • returns: dynamic - clone of this object.

fromJson

Initialize this object from JSON Map object

void fromJson(Map<String, dynamic> json)

  • json - json map object

get

Gets a map’s element specified by its key.

dynamic get(String key)

  • key: String - key of the element to get.
  • returns: dynamic - value of the map’s element.

getValue

Gets an map with values.

Map<dynamic, dynamic> getValue()

  • returns: Map<dynamic, dynamic> - the value of the map elements.

getAsArray

Converts a map’s element into an AnyValueArray object or returns an empty AnyValueArray object if theconversion is not possible.
See AnyValueArray, AnyValueArray.fromValue

AnyValueArray getAsArray(String key)

  • key: String - key of the element to get.
  • returns: AnyValueArray - AnyValueArray value of the element or empty AnyValueArray object if the conversion is not supported.

getAsArrayWithDefault

Converts a map’s element into an AnyValueArray object or returns a given default value if the conversion is not possible.
See AnyValueArray

AnyValueArray getAsArrayWithDefault(String key, AnyValueArray defaultValue)

  • key: String - key of element to get.
  • defaultValue: AnyValueArray - default value
  • returns: AnyValueArray - AnyValueArray value of the element or default value if the conversion is not supported.

getAsBoolean

Converts a map’s element into a boolean or returns false if the conversion is not possible.

bool getAsBoolean(String key)

  • key: String - key of the element to get.
  • returns: bool - value of the element or false if the conversion is not supported.

getAsBooleanWithDefault

Converts a map’s element into a boolean or returns a given default value if the conversion is not possible.
See BooleanConverter.toBooleanWithDefault

bool getAsBooleanWithDefault(String key, bool defaultValue)

  • key: String - key of the element to get.
  • defaultValue: bool - default value
  • returns: bool - boolean value of the element or given default value if the conversion is not supported.

getAsDateTime

Converts a map’s element into Date or returns the current date if the conversion is not possible.

DateTime getAsDateTime(String key)

  • key: String - key of the element to get.
  • returns: DateTime - Date value of the element or the current date if the conversion is not supported.

getAsDateTimeWithDefault

Converts a map’s element into a Date or returns a given default value if the conversion is not possible.
See DateTimeConverter.toDateTimeWithDefault

DateTime getAsDateTimeWithDefault(String key, DateTime defaultValue)

  • key: String - key of the element to get.
  • defaultValue: DateTime - default value
  • returns: DateTime - Date value of the element or given default value if the conversion is not supported.

getAsDouble

Converts a map’s element into a double or returns 0 if the conversion is not possible.

double getAsDouble(String key)

  • key: String - key of the element to get.
  • returns: double - double value of the element or 0 if the conversion is not supported.

getAsDoubleWithDefault

Converts a map’s element into a double or returns a given default value if the conversion is not possible.
See DoubleConverter.toDoubleWithDefault

double getAsDoubleWithDefault(String key, double defaultValue)

  • key: String - key of the element to get.
  • defaultValue: double - default value
  • returns: double - double value of the element or given default value if the conversion is not supported.

getAsFloat

Converts a map’s element into a float or returns 0 if conversion the is not possible.

double getAsFloat(String key)

  • key: String - key of the element to get.
  • returns: double - float value of the element or 0 if the conversion is not supported.

getAsFloatWithDefault

Converts a map’s element into a float or returns a given default value if the conversion is not possible.
See FloatConverter.toFloatWithDefault

double getAsFloatWithDefault(String key, double defaultValue)

  • key: String - key of the element to get.
  • defaultValue: double - default value
  • returns: double - flaot value of the element or given default value if the conversion is not supported.

getAsInteger

Converts a map’s element into an integer or returns 0 if the conversion is not possible.

int getAsInteger(String key)

  • key: String - key of the element to get.
  • returns: int - integer value of the element or 0 if the conversion is not supported.

getAsIntegerWithDefault

Converts a map’s element into an integer or returns a given default value if the conversion is not possible.
See IntegerConverter.toIntegerWithDefault

int getAsIntegerWithDefault(String key, int defaultValue)

  • key: String - key of the element to get.
  • defaultValue: int - default value
  • returns: int - integer value of the element or given default value if the conversion is not supported.

getAsLong

Converts a map’s element into a long or returns 0 if the conversion is not possible.
See IntegerConverter.toIntegerWithDefault

int getAsLong(String key)

  • key: String - key of the element to get.
  • returns: int - long value of the element or 0 if the conversion is not supported.

getAsLongWithDefault

Converts a map’s element into a long or returns a given default value if the conversion is not possible.
See LongConverter.toLongWithDefault

int getAsLongWithDefault(String key, int defaultValue)

  • key: String - key of the element to get.
  • defaultValue: int - default value
  • returns: int - long value of the element or default value if the conversion is not supported.

getAsMap

Converts a map’s element into an AnyValueMap object or returns an empty AnyValueMap object if the conversion is not possible.
See LongConverter.toLongWithDefault

AnyValueMap getAsMap(String key)

  • key: String - key of the element to get.
  • returns: AnyValueMap - AnyValueMap value of the element or empty AnyValueMap object if the conversion is not supported.

getAsMapWithDefault

Converts a map’s element into an AnyValueMap object or returns a given default value if the conversion is not possible.

AnyValueMap getAsMapWithDefault(String key, AnyValueMap defaultValue)

  • key: String - key of the element to get.
  • defaultValue: AnyValueMap - default value
  • returns: AnyValueMap - AnyValueMap value of the element or given default value if the conversion is not supported.

getAsNullableArray

Converts a map’s element into an AnyValueArray object or returns null if the conversion is not possible.
See AnyValueMap, AnyValueMap.fromValue

AnyValueMap? getAsNullableArray(String key)

  • key: String - key of the element to get.
  • returns: AnyValueMap? - AnyValueArray value of the element or null if the conversion is not supported.

getAsNullableBoolean

Converts a map’s element into a boolean or returns null if the conversion is not possible.
See BooleanConverter.toNullableBoolean

bool? getAsNullableBoolean(String key)

  • key: String - key of the element to get.
  • returns: bool? - boolean value of the element or null if the conversion is not supported.

getAsNullableDateTime

Converts map element into a long or returns a given default value if the conversion is not possible.
See DateTimeConverter.toNullableDateTime

DateTime? getAsNullableDateTime(String key)

  • key: String - key of element to get.
  • returns: DateTime? - Date value of the element or null if the conversion is not supported.

getAsNullableDouble

Converts a map’s element into a double or returns null if the conversion is not possible.
See DoubleConverter.toNullableDouble

double? getAsNullableDouble(String key)

  • key: String - key of element to get.
  • returns: double? - double value of the element or null if the conversion is not supported.

getAsNullableDuration

Converts map element into a Duration or returns null if conversion is not possible.

See DurationConverter.toNullableDuration

Duration? getAsNullableDuration(String key)

  • key: String - a key of element to get.
  • returns: Duration? - Duration value of the element or null if conversion is not supported.

getAsNullableFloat

Converts a map’s element into a float or returns null if the conversion is not possible.
See FloatConverter.toNullableFloat

double? getAsNullableFloat(String key)

  • key: String - key of the element to get.
  • returns: double? - float value of the element or null if the conversion is not supported.

getAsNullableInteger

Converts a map’s element into an integer or returns null if the conversion is not possible.
See IntegerConverter.toNullableInteger

int? getAsNullableInteger(String key)

  • key: String - key of element to get.
  • returns: int? - integer value of the element or null if the conversion is not supported.

getAsNullableLong

Converts a map’s element into a long or returns null if conversion is not possible.
See LongConverter.toNullableLong

int? getAsNullableLong(String key)

  • key: String - key of the element to get.
  • returns: int? - long value of the element or null if the conversion is not supported.

getAsNullableMap

Converts a map’s element into an AnyValueMap object or returns null if the conversion is not possible.

AnyValueMap? getAsNullableMap(String key)

  • key: String - key of the element to get.
  • returns: AnyValueMap? - AnyValueMap value of the element or null if the conversion is not supported.

getAsNullableString

Converts a map’s element into a string or returns null if the conversion is not possible.
See StringConverter.toNullableString

String? getAsNullableString(String key)

  • key: String - key of the element to get.
  • returns: String? - string value of the element or null if the conversion is not supported.

getAsNullableType

Converts a map element into a value defined by a specified typecode. If the conversion is not possible, it returns null.
See TypeConverter.toNullableType

T? getAsNullableType<T>(TypeCode type, String key)

  • type: TypeCode - TypeCode that defines the type of the result.
  • key: String - key of the element to get.
  • returns: T? - element’s value defined by the type code or null if the conversion is not supported.

getAsObject

Gets the value stored in a map’s element without any conversions. When the element’s key is not defined, it returns the entire map value.

dynamic getAsObject([String? key])

  • key: String? - (optional) key of the element to get
  • returns: dynamic - element’s value or value of the map when the index is not defined.

getAsString

Converts a map’s element into a string or returns "" if the conversion is not possible.

String getAsString(String key)

  • key: String - key of the element to get.
  • returns: String - string value of the element or "" if the conversion is not supported.

getAsStringWithDefault

Converts a map’s element into a string or returns a given default value if the conversion is not possible.
See StringConverter.toStringWithDefault

String getAsStringWithDefault(String key, String defaultValue)

  • key: String - key of the element to get.
  • defaultValue: String - default value
  • returns: String - string value of the element or given default value if the conversion is not supported.

getAsType

Converts a map’s element into a value defined by a specified type code. If the conversion is not possible, it returns the default value for the specified type.

T? getAsType<T>(TypeCode type, String key)

  • type: TypeCode - TypeCode that defines the type of the result.
  • key: String - key of the element to get.
  • returns: T? - element’s value defined by the typecode or default if the conversion is not supported.

getAsTypeWithDefault

Converts a map’s element into a value defined by a specified type code. If the conversion is not possible, it returns a given default value.
See TypeConverter.toTypeWithDefault

T getAsTypeWithDefault<T>(TypeCode type, String key, T defaultValue)

  • type: TypeCode - TypeCode that defines the type of the result
  • key: String - key of element to get.
  • defaultValue: T - default value
  • returns: T - element’s value defined by the typecode or default value if the conversion is not supported.

getAsValue

Converts a map’s element into an AnyValue object or returns an empty AnyValue object if the conversion is not possible.
See AnyValue, AnyValue.constructors

AnyValue getAsValue(String key)

  • key: String - key of the element to get.
  • returns: AnyValue -AnyValue value of the element or empty AnyValue object if the conversion is not supported.

getKeys

Gets the keys of all elements stored in this map.

List<String> getKeys()

  • returns: List<String> - list with all keys.

put

Puts a new value into a map’s element specified by its key.

void put(String key, dynamic value)

  • key: String - key of the element to put.
  • value: dynamic - new value for map’s element.

remove

Removes a map’s element specified by its key.

@override

void remove(dynamic key)

  • key: dynamic - key of the element to remove.

setAsObject

Sets a new value to a map’s element specified by its index. When the index is not defined, it resets the entire map value. This method has a double purpose because method overrides are not supported in JavaScript.

void setAsObject(key, [value])

  • key: dynamic - (optional) key of the element to set
  • value: dynamic - new element or map value.

toJson

Convert JSON Map object from values of this object

Map<dynamic, dynamic> toJson()

  • returns: Map<dynamic, dynamic> - JSON Map object

toString

Gets a string representation of the object. The result is a semicolon-separated list of key-value pairs as “key1=value1;key2=value2;key=value3”

String toString()

  • returns: String - string representation of the object.

Static methods

fromMaps

Creates a new AnyValueMap object by merging two or more maps. Maps defined later in the list override values from previously defined maps.

@override

static AnyValueMap fromMaps(List? maps)

  • maps: List? - array of maps to be merged
  • returns: AnyValueMap - newly created AnyValueMap object.

fromTuples

Creates a new AnyValueMap object from a list of key-value pairs called tuples.

static AnyAnyValueMapValueMap fromTuples(List tuples)

  • tuples: List - list of values where odd elements are keys and the following even elements are values.
  • returns: AnyValueMap - newly created AnyValueArray object.

fromTuplesArray

Creates a new AnyValueMap object from a list of key-value pairs called tuples. The method is similar to fromTuples but tuples are passed as array instead of parameters.

static AnyValueMap fromTuplesArray(List? tuples)

  • tuples: List? - list of values where odd elements are keys and the following even elements are values.
  • returns: AnyValueMap - newly created AnyValueArray object.

fromValue

Converts a specified value into an AnyValueMap object.

static AnyValueMap fromValue(dynamic value)

  • value: dynamic - value to be converted
  • returns: AnyValueMap - newly created AnyValueMap object.

Examples

var value1 =  AnyValueMap({ key1: 1, key2: '123.456', key3: '2018-01-01' });

value1.getAsBoolean('key1');   // Result: true
value1.getAsInteger('key2');   // Result: 123
value1.getAsFloat('key2');     // Result: 123.456
value1.getAsDateTime('key3');  // Result:  Date(2018,0,1)

See also