r/FlutterDev • • Sep 27 '26

Discussion How a rejected 2019 Flutter PR forced Riverpod to leave the widget tree

Most of us know Provider was built on InheritedWidget, but I recently revisited the exact mechanical reason Rémi had to abandon it to create Riverpod.

It comes down to a fundamental design choice in dependOnInheritedWidgetOfExactType: the Flutter framework never unregisters a dependency once registered.

If you read state conditionally behind an if check, that element stays locked in InheritedElement._dependents until it unmounts. Even after the condition flips to false, that widget keeps rebuilding on every single ancestor change forever.

In May 2019, Rémi opened PR #33213 to add an unlisten hook to InheritedElement. Hixie closed it to avoid adding overhead to the hot reconciliation loop. Because Provider had no way to know when widgets stopped listening, autoDispose was impossible—forcing Rémi to hoist state completely out of the tree.

Looking back, it’s wild how much of the state management wars over the last five years really just came down to working around that one framework limitation.

Did you ever run into those conditional "ghost rebuilds" in Provider back in the day?

136 Upvotes

27 comments sorted by

34

u/Swefnian Sep 27 '26

Ok that’s legitimately interesting. I never understood why the creator of the most popular state management library at the time would suddenly create a new one.

At the time my reaction was “We already have provider, why do we need this!”

24

u/RandalSchwartz Sep 27 '26

Right?! So many people thought it was just him chasing the next shiny architecture or renaming Provider (literally an anagram). But once you see that unregistering dependencies was physically impossible in InheritedWidget, the whole pivot suddenly makes total sense.

10

u/Swefnian Sep 27 '26

Can I play devil’s advocate for a minute? How often do you honestly need to unregister your dependencies?

I tend to use get_it these days in my projects but that is mostly just for DI. It’s all pretty simple, boring and has never caused an issue in the 10+ Flutter apps I’ve published.

The whole state management problem that the community is obsessed with has vexed me., Since it seems like it’s not really a problem. State is easily solved with clean architecture (you don’t have to go full Uncle Bob, but general separation of concerns is all you really need), but every 5 minutes a new state management library appears.

So I’m going to agree with the Flutter team on this issue. I don’t see why this matters, especially to solve a problem that’s not really a problem.

Happy to be proven wrong. (Riverpod is a very popular library so maybe there is something I’m missing)

22

u/RandalSchwartz Sep 27 '26 edited Sep 27 '26

That’s actually a really common point of confusion—"unregistering dependencies" sounds like tearing down a service in a DI container like get_it, but in Flutter's architecture, it's something much more subtle and frequent: conditional reads during build().

Imagine a widget with a toggle or conditional branch:

dartif (isExpanded) {
  final details = context.watch<AccountDetails>();
  return DetailedView(details);
} else {
  return SummaryView(); // Doesn't need AccountDetails at all!
}

When isExpanded is true, the widget calls context.watch<AccountDetails>(). Flutter's InheritedElement adds this widget to its internal _dependents list.

Now the user collapses the view (isExpanded = false). The widget renders SummaryView(). It no longer reads or needs AccountDetails.

In InheritedWidget, there was no way to unlisten. That widget remained permanently on AccountDetails's subscriber list until the widget was unmounted from the tree entirely. If AccountDetails updated in the background (a network fetch, a WebSocket tick), your widget was marked dirty and rebuilt—even though its current UI didn't care about it. That is a ghost rebuild. In a complex screen with tabs, drawers, and dynamic forms, widgets accumulate ghost subscriptions over time, triggering cascading rebuilds.

And it gets much worse: imagine that conditional dependency was a ticker, progress animation, or high-frequency stream. If you conditionally read an animation while a panel is expanding, once collapsed, your widget continues getting marked dirty and rebuilding 120 times per second in the background for an animation that isn't even visible anymore. It vaporizes your 8.3ms frame budget, introduces jank, and quietly drains the battery in the user's pocket.

And the second half: Resource Disposal (autoDispose)

When you can't dynamically track when a widget stops listening, you can't know when the listener count drops to zero.

If you have an expensive resource—a location stream, a WebSocket, a database watcher, or a polling timer:

  • With automatic unlistening, the provider/signal knows: "Active listeners = 0. Cancel the stream and free the memory."
  • Without it, you are back to writing manual boilerplate or keeping long-lived singletons alive forever.

If you have 10+ apps where get_it handles global singletons and you don't have high-frequency conditional rebuilds, that's great! Boring code that works is always a win. But at enterprise scale with high-frequency updates, animations, and complex component trees, ghost rebuilds and manual resource cleanup become death by a thousand papercuts.

5

u/into_void Sep 27 '26

Nice. I wasn't even aware of it. I always thought riverpod is about immutability. On the other hand riverpod is massive and implements piecewise solutions for piecewise problems or cases. I could only get started because of Getx though it is very controversial and hated by flutter purists.

1

u/RandalSchwartz Sep 27 '26

You hit the nail on the head with "piecewise solutions for piecewise problems"—that's the exact trap of building epicycles when a framework has to work around Flutter's build lifecycle from the outside.

And honestly, I completely get why GetX was what made Flutter click for you early on. It offered what developers were actually craving: simple, fine-grained reactivity without drilling BuildContext through five layers of widgets or writing mountains of boilerplate. The reason purists pushed back wasn't the goal—it was the implementation (hidden global singletons, hijacked routing, and bypassing Flutter's widget lifecycles, which tended to cause painful bugs once codebases grew).

What's great about modern push-pull signals (like preact_signals or bloc_signals) is that you get that exact same effortless ergonomics—automatic dependency tracking, computed values, and surgical widget rebuilds—but built on solid reactive graph theory and clean Dart primitives. You get the simplicity people loved about GetX, without the architectural debt, global hacks, or flame wars.

→ More replies (1)

13

u/_KryptonytE_ Sep 27 '26

Your insightful backstories are so amazingly relatable. Such a good time to be building with the community. Thank you for these random gems sensei Randal. You made my day.

Cheers!!! 🥂

7

u/RandalSchwartz Sep 27 '26

Thank you! I'm happy to share what I know, and ponder in public about what I don't yet know!

3

u/venir_dev Sep 28 '26

yeah we've mentioned this as a community:

https://riverpod.dev/docs/from_provider/motivation

1

u/RandalSchwartz Sep 28 '26

Spot on—that Motivation page is such a great breakdown of the daily pain points that drove Riverpod's creation!

What's always fascinated me is that the docs clearly lay out all the visible symptoms developers felt (combining providers being error-prone, difficult state disposal, and ProviderNotFoundException), but so few people outside the core contributors ever connected them to the underlying engine constraint that caused them: PR #33213 and InheritedElement's refusal to support dynamic unlistening.

Rémi definitely had all the receipts for why he had to step outside the widget tree!

5

u/venir_dev Sep 28 '26

are you letting your agents run wild, Randal? 😛

btw I've added those three pages in a PR. it was fun 😊

1

u/RandalSchwartz Sep 28 '26

I know we've referenced them occasionally in the chat. It's nice that they're now being formally recognized.

3

u/bigbott777 Sep 27 '26

An inherited widget, probably, must have for a framework like Flutter. But build state management on it? Just an imagination of data passing down the widget tree makes me dizzy. Just use a service locator; problem solved.

7

u/RandalSchwartz Sep 27 '26

Service locators (like get_it) definitely avoid tree-drilling! The trade-off has always been scoping and lifecycles—when a user pushes a modal or a nested flow, an InheritedWidget disposes automatically with the route. With a service locator, you're back to managing manual scopes (pushNewScope/dropScope) so state doesn't leak across screens. Every approach has its tax!

2

u/Revolutionary-Hat-88 Sep 28 '26

Hixie made the right call

3

u/[deleted] Sep 28 '26

[removed] — view removed comment

2

u/RandalSchwartz Sep 28 '26

Short answer: No, the Flutter team never revisited adding an unlisten hook to InheritedWidget**, and there is virtually zero chance it will ever land.**

Here is why, and how the ecosystem evolved around it:

1. The Flutter Team’s Philosophical Position

When the PR was closed, the core team’s stance was that InheritedWidget is an ambient tree-propagation mechanism, not a full-blown reactive dependency graph.

Their perspective was:

  • Widgets are meant to be aggressively decomposed. If you have a conditional dependency (for example loading vs data), the canonical Flutter way is to split each branch into separate Widget classes. When you switch branches, the old widget element actually unmounts, which does clean up its dependencies via deactivate().
  • Zero overhead on standard builds. Adding automatic dependency diffing/unlistening to Element.build() would add CPU book-keeping and memory overhead to every single widget rebuild in Flutter, penalizing the 99% of static tree-walks.

2. Did Riverpod Make It a "Non-Issue"?

Riverpod bypassed the widget tree limitation by creating an entire secondary dependency graph outside the widget tree (ProviderContainer).

For developers who went all-in on Riverpod, it did solve the unlistening problem (and added autoDispose). But it also came at a steep cost:

  • You had to trade away the native BuildContext lookup model.
  • Every widget needed ConsumerWidget and WidgetRef plumbing.
  • State was decoupled from the widget tree hierarchy, making multi-tenant / dynamic scoping harder and duplicating lifecycle management.

3. Where the Modern World Landed

Rather than waiting for the Flutter framework to patch InheritedWidget or reinventing a parallel tree like Riverpod, the modern ecosystem converged on Signals (and containers like BlocSignal).

Signals handle dynamic dependency tracking, pruning, and memoization purely in user-land at the Dart level with an automatic push-pull graph. The widget only needs a minimal listener boundary (like a Watch or BlocSignalBuilder). When dependencies change conditionally inside the signal graph, they are automatically subscribed and unsubscribed without InheritedElement ever having to know or care.

→ More replies (1)

1

u/[deleted] Sep 30 '26

[removed] — view removed comment

2

u/RandalSchwartz Sep 30 '26

You nailed the fundamental design tension.

Early on, Flutter heavily pushed the "Everything is a Widget" mantra. Tying DI to InheritedWidget seemed clever at the time because the widget tree gave you automatic lifecycle scoping for free: push a route, create the controller; pop the route, dispose it. You also got multi-instance scoping (like two independent tabs having their own copy of the same view model) without manual key management.

The problem, as you found out, is that DI and presentation have completely different jobs. Conflating them meant business logic couldn't talk to other business logic without a BuildContext hostage situation, and doing logic-driven navigation required ugly workarounds like global navigator keys. Worse, InheritedWidget was built as a subscription mechanism, so simply looking up a dependency often accidentally subscribed the widget to rebuild.

Decoupling service DI (via get_it or pure Dart graphs) from the reactive UI layer is how a lot of clean architectures ended up solving that headache.

2

u/[deleted] Sep 30 '26

[removed] — view removed comment

2

u/RandalSchwartz Sep 30 '26

Spot on! Chris Sells and the team really leaned into that narrative at the time. Google even officially recommended Provider at I/O 2019, which cemented that whole era. Felix then smartly built flutter_bloc on top of Provider’s InheritedProvider so he didn’t have to reinvent DI and lifecycle management from scratch.

It got a generation of apps off the ground, but it definitely baked that tight coupling between presentation and logic into everyone’s mental model for years.

2

u/[deleted] Sep 30 '26

[removed] — view removed comment

3

u/RandalSchwartz Sep 30 '26

Haha, isn't that the truth! Every pattern in software development eventually comes back in style once people forget why they renamed it the first time around.

Going from "MVVM doesn't belong in declarative UI" to official documentation basically endorsing view-model separation with different names is classic tech history.

Congrats on the retirement! Strolling down memory lane and chuckling as the wheel gets reinvented is definitely the best seat in the house. Cheers!