Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Flutter build stops because BuildContext has no inheritFromWidgetOfExactType

ErrorError: 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.

  • Applies to Flutter 3.x, Dart 3.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 analyze
  2. 2Match what you see, then jump to its fix

    1. 1The error path points at a file under lib/ in your projectYour own code still calls the removed methodFix
    2. 2The error path contains .pub-cache/hosted/pub.dev/<package>-<version>/An old package calls it and has a newer releaseFix
    3. 3dozens of the same error across your projectMany call sites to migrate in your own codeFix

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 analyze

Confirmed by docs.flutter.dev

Did this fix it?

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 run

Confirmed by docs.flutter.dev

Did this fix it?

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-run

2Fix

Save all files, then apply the fixes the tool proposes.

dart fix --apply

3Verify

Run the analyzer and review the diff.

flutter analyze

Confirmed by dart.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.