StreamProvider<T> class
Creates a stream and expose its latest event.
StreamProvider is identical in behavior/usage to FutureProvider, modulo the fact that the value created is a Stream instead of a Future.
It can be used to express a value asynchronously loaded that can change over
time, such as an editable Message
coming from a web socket:
final messageProvider = StreamProvider.autoDispose<String>((ref) async* {
// Open the connection
final channel = IOWebSocketChannel.connect('ws://echo.websocket.org');
// Close the connection when the stream is destroyed
ref.onDispose(() => channel.sink.close());
// Parse the value received and emit a Message instance
await for (final value in channel.stream) {
yield value.toString();
}
});
Which the UI can then listen:
Widget build(BuildContext, ScopedReader watch) {
AsyncValue<String> message = watch(messageProvider);
return message.when(
loading: () => const CircularProgressIndicator(),
error: (err, stack) => Text('Error: $err'),
data: (message) {
return Text(message);
},
);
}
Note: When listening to web sockets, firebase, or anything that consumes resources, it is important to use StreamProvider.autoDispose instead of simply StreamProvider.
This ensures that the resources are released when no-longer needed as, by default, a StreamProvider is almost never destroyed.
See also:
- Provider, a provider that synchronously creates a value
- FutureProvider, a provider that asynchronously expose a value which can change over time.
- StreamProvider.stream, to obtain the Stream created instead of an AsyncValue.
- StreamProvider.last, to obtain the last value emitted by a Stream.
- StreamProvider.family, to create a StreamProvider from external parameters
- StreamProvider.autoDispose, to destroy the state of a StreamProvider when no-longer needed.
- Inheritance
-
- Object
- ProviderBase<
Stream< T> , AsyncValue<T> > - RootProvider<
Stream< T> , AsyncValue<T> > - AlwaysAliveProviderBase<
Stream< T> , AsyncValue<T> > - StreamProvider
- Annotations
-
- @sealed
Constructors
-
StreamProvider(Create<
Stream< _create, {String? name})T> , ProviderReference> - Creates a stream and expose its latest event.
Properties
- argument → Object?
-
If this provider was created with the
.family
modifier, argument is variable used.no setterinherited - debugId ↔ String
-
A unique identifier for this provider, used by devtools to differentiate providers
latefinalinherited
-
from
→ Family<
dynamic, dynamic, dynamic, ProviderReference, RootProvider> ? -
If this provider was created with the
.family
modifier, from is the.family
instance.no setterinherited - hashCode → int
-
The hash code for this object.
no setterinherited
-
last
→ AlwaysAliveProviderBase<
Object?, Future< T> > -
Exposes a
Future
which resolves with the last value or error emitted.no setter - name → String?
-
A custom label for providers.
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
stream
→ AlwaysAliveProviderBase<
Stream< T> , Stream<T> > -
Exposes the
Stream
created by a StreamProvider.no setter
Methods
-
create(
ProviderReference ref) → Stream< T> -
override
-
createElement(
) → ProviderElement< Stream< T> , AsyncValue<T> > -
An internal method that defines how a provider behaves.
inherited
-
createState(
) → _StreamProviderState< T> -
An internal method that creates the state of a provider.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
overrideWithProvider(
AlwaysAliveProviderBase< Object?, AsyncValue< provider) → ProviderOverrideT> > -
Overrides the behavior of this provider with another provider.
inherited
-
overrideWithValue(
AsyncValue< T> value) → Override -
inherited
-
select<
Selected> (Selected selector(AsyncValue< T> value)) → ProviderListenable<Selected> -
Partially listen to a provider.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
- autoDispose → const AutoDisposeStreamProviderBuilder
- Marks the provider as automatically disposed when no-longer listened.
- family → const StreamProviderFamilyBuilder
- A group of providers that builds their value from an external parameter.