Flutter build stops because BuildContext has no inheritFromWidgetOfExactType
Error: The method 'inheritFromWidgetOfExactType' isn't defined for the class 'BuildContext'.Short answer
The method was renamed, then removed in Flutter 2.0. Replace inheritFromWidgetOfExactType(MyType) with the generic dependOnInheritedWidgetOfExactType<MyType>(). If the call sits inside a package under .pub-cache, you cannot edit it sensibly: upgrade that package to a release written for your Flutter version, or run dart fix.
Fix it fast
1Find out which cause you have
Read the file path in the first line of the error. A path inside your own lib folder means your code; a path containing .pub-cache means a dependency.
flutter analyze2Match what you see, then jump to its fix
- 1
The error path points at a file under lib/ in your projectYour own code still calls the removed methodFix - 2
The error path contains .pub-cache/hosted/pub.dev/<package>-<version>/An old package calls it and has a newer releaseFix - 3
dozens of the same error across your projectMany call sites to migrate in your own codeFix
- 1
What does the error look like?
Error: The method 'inheritFromWidgetOfExactType' isn't defined for the class 'BuildContext'.
Cause 1 of 3
Your own code still calls the removed method
1Check
Search your sources for the old names. Both inheritFromWidgetOfExactType and inheritFromElement were removed from BuildContext and Element.
grep -rn "inheritFromWidgetOfExactType\|inheritFromElement" lib/2Fix
Use the new names. The type moves out of the argument list and into angle brackets, and the call returns a nullable value.
// before
final inherited = context.inheritFromWidgetOfExactType(MyInherited) as MyInherited?;
// after
final inherited = context.dependOnInheritedWidgetOfExactType<MyInherited>();3Verify
Analyze again; the error is gone and the message count drops.
flutter analyzeConfirmed by docs.flutter.dev
Cause 2 of 3
An old package calls it and has a newer release
1Check
See which version of that package you resolve to, and whether a newer one exists.
flutter pub deps | grep -i "<package>"2Fix
Raise the package constraint in pubspec.yaml to a release that supports your Flutter version and fetch it. If the package is no longer maintained, move to a maintained alternative.
flutter pub upgrade <package>3Verify
Build again after the upgrade.
flutter runConfirmed by docs.flutter.dev
Cause 3 of 3
Many call sites to migrate in your own code
1Check
The Flutter migration page marks this rename as supported by the fix tool, so Dart can rewrite it. Preview first.
dart fix --dry-run2Fix
Save all files, then apply the fixes the tool proposes.
dart fix --apply3Verify
Run the analyzer and review the diff.
flutter analyzeConfirmed by dart.dev
How do I stop it coming back?
- Run flutter analyze in CI so a removed API fails a pull request, not a release build.
- Read the breaking-changes page before moving to a new Flutter major version.
- Prefer packages with recent releases; check the version listed on pub.dev before adding one.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix