close

[Solved] Error: The method ‘inheritFromWidgetOfExactType’ isn’t defined for the class ‘BuildContext’

Hello Guys How are you all? Hope You all are fine. When I was trying to run my flutter app and suddenly I get the following error in my stack track. Error: The method ‘inheritFromWidgetOfExactType’ isn’t defined for the class ‘BuildContext’ in flutter. So today Here I come with all possible solutions for this error.

We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.

How Error: The method ‘inheritFromWidgetOfExactType’ isn’t defined for the class ‘BuildContext’ Occurs ?

Today When I un My app suddenly I Got Below Error in my console.

This will generate a JSON format file containing all messages that 
need to be translated.

../../../.pub-cache/hosted/pub.flutter-io.cn/pull_to_refresh-1.6.3/lib/src/smart_refresher.dart:273:21: Error: The method 'ancestorWidgetOfExactType' isn't defined for the class 'BuildContext'.
 - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('../../../apps/flutter/packages/flutter/lib/src/widgets/framework.dart').
Try correcting the name to the name of an existing method, or defining a method named 'ancestorWidgetOfExactType'.
    return context?.ancestorWidgetOfExactType(SmartRefresher);
                    ^^^^^^^^^^^^^^^^^^^^^^^^^

Also I Don’t have any error in my Flutter Doctor Commands output.

How to Solve Error: The method ‘inheritFromWidgetOfExactType’ isn’t defined for the class ‘BuildContext’ ?

  1. How to Solve Error: The method 'inheritFromWidgetOfExactType' isn't defined for the class 'BuildContext' ?

    to Solve Error: The method 'inheritFromWidgetOfExactType' isn't defined for the class 'BuildContext' As you can see error is in this Path ./.pub-cache/hosted/pub.flutter-io.cn/pull_to_refresh-1.6.3/lib/src/smart_refresher.dart:273:21. This path probably one of my plugin pull_to_refresh. when I see in my pubspec.yaml I was using pull_to_refresh plugins old Version. So that I was just upgrade my plugin version. and My Error Was gone.

  2. Error: The method 'inheritFromWidgetOfExactType' isn't defined for the class 'BuildContext'

    to Solve Error: The method 'inheritFromWidgetOfExactType' isn't defined for the class 'BuildContext' As you can see error is in this Path ./.pub-cache/hosted/pub.flutter-io.cn/pull_to_refresh-1.6.3/lib/src/smart_refresher.dart:273:21. This path probably one of my plugin pull_to_refresh. when I see in my pubspec.yaml I was using pull_to_refresh plugins old Version. So that I was just upgrade my plugin version. and My Error Was gone.

Solution 1 : Just Update Your Plugin Version

As you can see error is in this Path ./.pub-cache/hosted/pub.flutter-io.cn/pull_to_refresh-1.6.3/lib/src/smart_refresher.dart:273:21

This path probably one of my plugin pull_to_refresh. when I see in my pubspec.yaml I was using pull_to_refresh plugins old Version. So that I was just upgrade my plugin version. and My Error Was gone.

So Possible Solution Is Just Upgrade plugin version will resolve your Error. I Was find solution everywhere till 2 days but no any solution worked. But By just updating Plugin was solved my issue.

Solution 2 : Use dependOnInheritedWidgetOfExactType

If Solution 1 is Not worked then Its Okay. Just Try This Solution.

This error is caused by the inheritFromWidgetOfExactType method. This method is deprecated .
Use dependOnInheritedWidgetOfExactType instead. Your project simply contains it.

Find inheritFromWidgetOfExactType method in your code and adapt it with dependOnInheritedWidgetOfExactType.

For Example.

Before

static Name of(BuildContext context) {
  return context.inheritFromWidgetOfExactType(Name);  //before
}

After

static Name of(BuildContext context) {
  return context.dependOnInheritedWidgetOfExactType<Name>();  //after
}

Solution 3 : downgrade your flutter version

If Above Both solution Not worked Then Simply downgrade your flutter version. Just Use below command.

flutter downgrade

OR

Switch to the beta channel

flutter channel beta

If you don’t have beta channel yet , you need to add flutter upgrade

Solution 4 : Update dependencies Version

For me was because I was using an old version of my dependencies. Check pub.dev to see if has a new version of your packages!

Summery

So it’s all About All possible solutions. Hope this above all solution helped you a lot. Comment below Your thoughts and your queries. Comment Below on your suggestion.

Check Out Below Article

1 thought on “[Solved] Error: The method ‘inheritFromWidgetOfExactType’ isn’t defined for the class ‘BuildContext’”

Leave a Comment

%d bloggers like this: