Day 16: Introduction to Provider and InheritedWidget

Flutter App Development: High-Fidelity Travel App Engineering ~7 min read 60 min hands-on

Lesson 16 — Introduction to Provider and InheritedWidget

NomadAir Masterclass · Module 2 · Beginner

The Problem setState Cannot Solve

Lesson 15 ended with AutomaticKeepAliveClientMixin keeping the search form alive across tab switches. The form values survive. But the moment the user navigates from SearchScreen to SearchResultsScreen, the criteria must be passed as a GoRouter extra. And when SearchResultsScreen needs to show a "Modify search" button that repopulates the form, the criteria must travel back up. This is prop drilling through navigation — structurally identical to prop drilling through widget nesting, just harder to see.

The three conditions from Lesson 15 evaluated against SearchCriteria:

  1. Local? No — both SearchScreen and SearchResultsScreen need it. Fails.

  2. Ephemeral? No — it must survive tab switches, route pushes, and will eventually persist to search history. Fails.

  3. Unshared? No — two screens share it. Fails.

All three conditions fail. setState is the wrong tool. Provider is the right tool for this specific problem.

What Provider Is — and What It Isn't

Provider is a wrapper around Flutter's InheritedWidget that removes the boilerplate of writing InheritedWidget subclasses by hand. Understanding InheritedWidget is therefore a prerequisite for using Provider correctly.

InheritedWidget is Flutter's mechanism for propagating data down the widget tree without passing it through every constructor. It uses BuildContext.dependOnInheritedWidgetOfExactType<T>() to find the nearest ancestor of type T and subscribe to its changes. When updateShouldNotify returns true, Flutter marks all dependent widgets as needing a rebuild.

ChangeNotifier is Flutter's built-in observable pattern. It maintains a list of VoidCallback listeners. When you call notifyListeners(), every registered listener fires. ChangeNotifierProvider combines these: it creates a ChangeNotifier, places it in an InheritedWidget in the tree, and calls notifyListeners() to trigger dependant rebuilds.

What Provider is: a dependency injection mechanism that makes a ChangeNotifier available anywhere below it in the widget tree, with automatic rebuild propagation.

What Provider is not: a complete state management solution. It does not handle async state (loading / error / data), it requires a BuildContext at every call site, it cannot be read outside the widget tree (in repositories or services), and it does not scale cleanly beyond ~5 providers in a large app. These limitations are precisely why Riverpod (Lesson 32) exists.

The NomadAir L16 Migration

Before (L15): SearchCriteria _criteria owned by _SearchScreenState with setState. Passed to SearchResultsScreen via GoRouter extra.

After (L16):

SearchCriteriaNotifier holds the entire SearchCriteria and exposes granular setters — setOrigin, setDestination, setDepartureDate, setReturnDate, setPassengers, setCabinClass, setRoundTrip, reset. Each setter calls notifyListeners().

Keep going with the full lesson

The rest of this lesson — plus its implementation guide, runnable scripts and source code — is included with the course or an All Access plan. Create a free account to start the free lessons in every course.

Already a member? Sign in