Use a provider of type StreamProvider to receive values of type T from a Stream.Ī StreamProvider is said to “subscribe” to a stream to receive the events that the stream emits. StreamProviderĪ StreamProvider receives events from a stream and provides these events to its dependent widgets as values representing state. After examining the basic kinds of providers, we’ll finally be in position to understand the entire architecture diagram. Basic Kinds of Providersĭifferent providers interact differently with state sources to inform their dependents of state changes. Let’s make this more concrete by examining the basic kinds of providers. The Flutter framework rebuilds the dependents previously marked for building, in the process acquiring the new state value as explained previously.The provider, via its internal InheritedWidget, marks for building the dependent widgets that are listening for changes in the provider’s value. This may also entail the provider retrieving the new state value from the state source, depending on the kind of state source. The state source informs the provider that the state has changed.For example, the above dependent that doesn’t rebuild might represent a button whose pressing causes a state change. Something induces a new state in the state source, such as an external event precipitating the change or an external object sending a new state value.The process of rebuilding dependents begins with a state change, as follows: Only three dependents in this diagram subsequently rebuild on state changes. If the state source has not supplied a state value by the time of this first build, the provider hands the dependents an application-supplied default initial state value. All of the dependents first build on an initial state value. The diagram shows four widgets that are dependent on a state value. The following diagram generically illustrates the process of rebuilding dependent widgets when a state source reports a state change: The provider package includes a variety of providers specialized for subscribing to various kinds of state sources. State sources include streams, futures, and instances of Listenable, among others. The provider package does not have a term for objects that do this, so we’ll call them “state sources.” A state source, then, is an object that reports state changes. If we’re going to rebuild widgets on state changes, we need something that reports changes in state values. Now that you are familiar with how providers provide values and rebuild dependents, let’s get a good sense of the basic kinds of providers. You can choose the provider having the efficiency you need and the economy of expression you desire. It has a slew of providers for working with a variety of sources, and it’s even possible to design your own specialized provider. The provider package stands out for how flexibly it supports different sources of state. This second article introduces the basic kinds of providers. It assumes the reader has read the first article, Part 1: Providing values.
#All that remains part 1 override series
This article is the second in a three-part series that describes the architecture of the Flutter provider package and illustrates this architecture in diagrams.