streamOne method
Creates a stream to the remote server to receive continuous updates
about all changes on the data model identified by id
.
Unlike watchOne
, which only fetches data once and then listens to the
local repository for changes, this method instead creates a permanent
connection to the remote server to get realtime changes of all
modifications of the server.
The stream itself will return the current state of the element, with each
modification leading to a new event with the new version of the data
model. In case of deletions, this means null
get emitted. In addition,
all changes are also persisted locally.
Typically, the stream stays alive until explicitly closed by the consumer. However, in some cases the server might encounter an error and decide to close the stream. In that case, a RemoteCancellation exception is thrown before the stream closes.
The parameters params
and headers
work the same way as for findAll.
The autoRenew
flag controls what happens if the stream is closed by the
server because the auth token has expired. If set to true
(the default),
the stream will automatically be recreated with a new auth token. When
disabled, is such cases a AuthenticationRevoked exception will be
thrown.
The final parameter, onUnsupportedEvent
can be used to implement custom
handling of server events that cannot be consumed by the stream. This can
happen, if for example data deep in the tree is modified, as the stream
cannot perform patches on properties of individual data models.
Implementation
// coverage:ignore-start
Stream<T?> streamOne(
String id, {
Map<String, dynamic>? params,
Map<String, String>? headers,
bool autoRenew = true,
UnsupportedEventCb? onUnsupportedEvent,
}) =>
StreamOneController<T>(
id: id,
createStream: () async => DatabaseEventStream(
uri: await generateGetUri(id, params),
headers: await generateHeaders(headers),
client: httpClient,
),
adapter: this,
autoRenew: autoRenew,
onUnsupportedEvent: onUnsupportedEvent,
).stream;