AutoDisposeStreamProvider<T> class Null safety

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://');

  // 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 {
    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:



AutoDisposeStreamProvider(Create<Stream<T>, AutoDisposeProviderReference> _create, {String? name})
Creates a stream and expose its latest event. [...]


argument Object?
If this provider was created with the .family modifier, argument is variable used.
read-only, inherited
debugId String
A unique identifier for this provider, used by devtools to differentiate providers [...]
late, final, inherited
from Family<dynamic, dynamic, dynamic, ProviderReference, RootProvider>?
If this provider was created with the .family modifier, from is the .family instance.
read-only, inherited
hashCode int
The hash code for this object. [...]
@nonVirtual, read-only, inherited
last AutoDisposeProviderBase<Object?, Future<T>>
Exposes a Future which resolves with the last value or error emitted. [...]
name String?
A custom label for providers. [...]
final, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
stream AutoDisposeProviderBase<Stream<T>, Stream<T>>
Exposes the Stream created by a StreamProvider. [...]


create(covariant AutoDisposeProviderReference ref) Stream<T>
createElement() AutoDisposeProviderElement<Stream<T>, AsyncValue<T>>
An internal method that defines how a provider behaves.
createState() → _AutoDisposeStreamProviderState<T>
An internal method that creates the state of a provider.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
overrideWithProvider(AutoDisposeProviderBase<Object?, AsyncValue<T>> provider) → ProviderOverride
Overrides the behavior of this provider with another provider. [...]
overrideWithValue(AsyncValue<T> value) Override
select<Selected>(Selected selector(AsyncValue<T> value)) ProviderListenable<Selected>
Partially listen to a provider. [...]
toString() String
A string representation of this object. [...]


operator ==(Object other) bool
The equality operator. [...]


family → const AutoDisposeStreamProviderFamilyBuilder
A group of providers that builds their value from an external parameter. [...]