본문으로 건너뛰기

StreamProvider

StreamProvider is similar to FutureProvider but for Streams instead of Futures.

StreamProvider is usually used for:

  • listening to Firebase or web-sockets
  • rebuilding another provider every few seconds

Since Streams naturally expose a way for listening to updates, some may think that using StreamProvider has a low value. In particular, you may believe that Flutter's StreamBuilder would work just as well for listening to a Stream, but this is a mistake.

Using StreamProvider over StreamBuilder has numerous benefits:

  • it allows other providers to listen to the stream using ref.watch.
  • it ensures that loading and error cases are properly handled, thanks to AsyncValue.
  • it removes the need for having to differentiate broadcast streams vs normal streams.
  • it caches the latest value emitted by the stream, ensuring that if a listener is added after an event is emitted, the listener will still have immediate access to the most up-to-date event.
  • it allows easily mocking the stream during tests by overriding the StreamProvider.