场景还原:

在flutter开发中,引入provide 1.0.2 包用于状态管理;运行是就出错了;一开始还很纳闷,明明好好的,怎么就说错就错了呢?于是在百度了一下,找不到,只能另寻其他办法,在stockoverflow上找到了解决方案,以供参考

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

flutter 版本  

H:\study\Flutter\flutter_app\flutter-api\api>flutter doctor -v
[√] Flutter (Channel stable, 2.0.3, on Microsoft Windows [Version 10.0.18363.1500], locale zh-CN)
    • Flutter version 2.0.3 at D:\development\flutter
    • Framework revision 4d7946a68d (7 weeks ago), 2021-03-18 17:24:33 -0700
    • Engine revision 3459eb2436
    • Dart version 2.12.2
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at C:\Users\Administrator\AppData\Local\Android\Sdk\
    • Platform android-30, build-tools 30.0.3
    • ANDROID_SDK_ROOT = C:\Users\Administrator\AppData\Local\Android\Sdk\
    • Java binary at: D:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 4.1.0)
    • Android Studio at D:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] Connected device (3 available)
    • SM G977N (mobile) • 127.0.0.1:62001 • android-x86    • Android 7.1.2 (API 25)
    • Chrome (web)      • chrome          • web-javascript • Google Chrome 90.0.4430.93
    • Edge (web)        • edge            • web-javascript • Microsoft Edge 89.0.774.77

• No issues found!

安装状态管理

 

dependencies:
  flutter:
    sdk: flutter
  provide: ^1.0.2

Update your project

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

找到  inheritFromWidgetOfExactType 这个方法所在的代码位置,并替换为 inheritFromWidgetOfExactType 

Example of a replacement

替换的代码案例如下

Before : with InheritFromWidgetOfExactType

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

Now with dependOnInheritedWidgetOfExactType (Recommanded)

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

Now instead of taking a Type as argument, The method is generic .
Brief <...>() instead of (...)

The error may also be caused by a package you have imported :

Solution: Check if this package has a new update

剩下的就去试试喽;

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐