DynamicContent class

Configuration data from the remote widgets.

Typically this represents the data model, and is updated frequently (or at least, more frequently than the remote widget definitions) by the server (or, indeed, by local code, in response to events or other activity).

Structure

A DynamicContent object represents a tree. A consumer (the remote widgets) can subscribe to a node to obtain its value.

The root of a DynamicContent tree is a map of string-value pairs. The values are:

The keys in the root map are independently updated. Typically each represents a different category of data from the server that the server updates independently, e.g. theming information and the app state might be provided separately.

Updates

Data is updated using update and updateAll. The objects passed to those methods are of the types described above.

Objects for update can be obtained in several ways:

  1. Dart maps, lists, and literals of the types given above ("raw data") can be created directly in code. This is useful for configuring remote widgets with local client information such as the current time, GPS coordinates, system settings like dark mode, window dimensions, etc, where the data was never encoded in the first place.

  2. A Remote Flutter Widgets binary data blob can be parsed using decodeDataBlob. This is the preferred method for decoding data obtained from the network. See encodeDataBlob for a function that generates data in this format.

  3. A Remote Flutter Widgets text data file can be parsed using parseTextDataFile. Decoding this text format is about ten times slower than decoding the binary format and about five times slower than decoding JSON, so it is discouraged in production applications. This text representation of the Remote Flutter Widgets binary data blob format is similar to JSON. This form is typically not used in applications; it is more common for this format to be used on the server side, parsed and then encoded in binary form for transmission to the client.

  4. Data in JSON form can be decoded using JsonCodec.decode (typically using the json object); the JSON decoder creates the same types of data structures as expected by update. This is not generally recommended but may be appropriate if the data was obtained from a third-party source in JSON form and could not be preprocessed by a server to convert the data to the binary form described above. Numbers in JSON are interpreted as doubles if they contain a decimal point or an e in their source representation, and as integers otherwise. This can cause issues as the DynamicContent and DataSource are strongly typed and distinguish int and double. Explicit nulls in the JSON are an error (the data format supported by DynamicContent does not support nulls). Decoding JSON is about 1.5x slower than the binary format.

Subscribers are notified immediately after an update if their data changed.

References

To subscribe to a node, the subscribe method is used. The method returns the current value. When the value later changes, the provided callback is invoked with the new value.

The unsubscribe method must be called when the client no longer needs updates (e.g. when the widget goes away).

To identify a node, a list of keys is used, giving the path from the root to the node. Each key is either a string (to index into maps) or an integer (to index into lists). If no node is identified, the missing value is returned. Similarly, if a node goes away, subscribers are given the value missing as the new value. It is not an error to subscribe to missing data. It is an error to add missing values to the data model, however.

To subscribe to the root of the DynamicContent, use an empty list as the key when subscribing.

The LocalWidgetBuilders passed to a LocalWidgetLibrary use a DataSource as their interface into the DynamicContent. To ensure the integrity of the update mechanism, that interface only allows access to leaves, not intermediate nodes (maps and lists).

It is an error to subscribe to the same key multiple times with the same callback.

Constructors

DynamicContent([DynamicMap? initialData])
Create a fresh DynamicContent object.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
subscribe(List<Object> key, SubscriptionCallback callback) Object
Obtain the value at location key, and subscribe callback to that key so that future updates will invoke the callback with the new value.
toString() String
A string representation of this object.
override
unsubscribe(List<Object> key, SubscriptionCallback callback) → void
Removes a subscription created by subscribe.
update(String rootKey, Object value) → void
Updates the content with the specified data.
updateAll(DynamicMap initialData) → void
Update all the keys in the DynamicContent.

Operators

operator ==(Object other) bool
The equality operator.
inherited