错误:类'BuildContext'没有定义方法'inheritFromWidgetOfExactType'。

38
今天当我编译我的Flutter项目时,出现了以下错误:
This will generate a JSON format file containing all messages that 
need to be translated.
../../../.pub-cache/hosted/pub.flutter-io.cn/fish_redux-0.3.4/lib/src/redux_component/page.dart:208:17: Error: The method 'inheritFromWidgetOfExactType' 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 'inheritFromWidgetOfExactType'.
        context.inheritFromWidgetOfExactType(PageProvider);
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../.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);
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
../../../.pub-cache/hosted/pub.flutter-io.cn/pull_to_refresh-1.6.3/lib/src/smart_refresher.dart:277:21: Error: The method 'ancestorStateOfType' 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 'ancestorStateOfType'.
    return context?.ancestorStateOfType(TypeMatcher<SmartRefresherState>());
                    ^^^^^^^^^^^^^^^^^^^
../../../.pub-cache/hosted/pub.flutter-io.cn/pull_to_refresh-1.6.3/lib/src/smart_refresher.dart:1003:20: Error: The method 'inheritFromWidgetOfExactType' 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 'inheritFromWidgetOfExactType'.
    return context.inheritFromWidgetOfExactType(RefreshConfiguration);
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../.pub-cache/hosted/pub.flutter-io.cn/chewie-0.9.10/lib/src/chewie_player.dart:276:17: Error: The method 'inheritFromWidgetOfExactType' 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 'inheritFromWidgetOfExactType'.
        context.inheritFromWidgetOfExactType(_ChewieControllerProvider)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../.pub-cache/hosted/pub.flutter-io.cn/chewie_audio-1.0.0+1/lib/src/chewie_player.dart:101:17: Error: The method 'inheritFromWidgetOfExactType' 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 'inheritFromWidgetOfExactType'.
        context.inheritFromWidgetOfExactType(_ChewieAudioControllerProvider)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

我正在使用Flutter的主频道,以下是Flutter Doctor的结果:
[✓] Flutter (Channel master, 1.26.0-2.0.pre.366, on Mac OS X 10.15.7 19H114 darwin-x64, locale en-CN)
    • Flutter version 1.26.0-2.0.pre.366 at /Users/dolphin/apps/flutter
    • Framework revision b7f6d9bcb2 (24 hours ago), 2021-01-15 07:44:03 -0500
    • Engine revision 69a7538a90
    • Dart version 2.12.0 (build 2.12.0-227.0.dev)
    • 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.2)
    • Android SDK at /Users/dolphin/Library/Android/sdk
    • Platform android-30, build-tools 30.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.3, Build version 12C33
    • CocoaPods version 1.10.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • 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-b3-6915495)

[✓] IntelliJ IDEA Community Edition (version 2020.3.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • 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

[✓] IntelliJ IDEA Ultimate Edition (version 2019.2.2)
    • IntelliJ at /Users/dolphin/Applications/JetBrains Toolbox/IntelliJ IDEA Ultimate.app
    • 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

[✓] VS Code (version 1.52.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.18.1

[✓] Connected device (3 available)
    • sdk gphone x86 arm (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • macOS (desktop)             • macos         • darwin-x64     • Mac OS X 10.15.7 19H114 darwin-x64
    • Chrome (web)                • chrome        • web-javascript • Google Chrome 87.0.4280.141
    ! Error: xiaoqiang 的 iPhone is not connected. Xcode will continue when xiaoqiang 的 iPhone is connected. (code
      -13)

• No issues found!
(base)

升级到Flutter 2.0.1后,再次出现以下错误:
~/Documents/GitHub/cruise-open on  feature/channel! ⌚ 15:01:43
$ ~/apps/flutter/bin/flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.1, on Mac OS X 10.15.7 19H114 darwin-x64, locale en-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] IntelliJ IDEA Community Edition (version 2020.3.2)
[✓] IntelliJ IDEA Ultimate Edition (version 2019.2.2)
[✓] VS Code (version 1.53.2)
[✓] Connected device (3 available)
    ! Error: xiaoqiang 的 iPhone is not connected. Xcode will continue when xiaoqiang 的 iPhone is connected. (code
      -13)

• No issues found!
(base)

从beta切换到master通道后仍然遇到相同的问题。如果您找到解决方案,请告诉我。 - user1032620
在几天前更新Flutter后,我遇到了相同的问题。我通过升级provider包来解决了一部分问题,但现在我面临着新的问题。 - Justin Klaus
2
我现在切换到稳定通道,也许将来升级后会遇到错误。@user1032620 - Dolphin
两者不幸都没有解决我的问题。在beta中构建没有问题,但需要主要通道才能避免最终在testflight中崩溃的应用程序。 - user1032620
有人找到解决方法了吗? - Shvet
6个回答

49

这个错误是由于 inheritFromWidgetOfExactType 方法引起的。

该方法已被弃用。请使用 dependOnInheritedWidgetOfExactType 代替。

您的项目中仅仅包含它

第一种解决方案:

手动返回到版本1.26.0-8.0或使用此命令:

flutter downgrade

第二种解决方案:

切换到测试版频道

flutter channel beta

如果您还没有 beta 渠道,您需要添加flutter upgrade

第三种解决方案:

更新您的项目

在您的代码中查找 inheritFromWidgetOfExactType 方法,并使用 dependOnInheritedWidgetOfExactType 进行适应。

替换示例:

之前的方式:使用 InheritFromWidgetOfExactType

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

现在使用dependOnInheritedWidgetOfExactType (推荐)

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

现在,该方法变为泛型方式,而不是以 Type 作为参数。使用 <...>() 替代 (...)

错误也可能是由您导入的软件包引起的:

解决方案: 检查此软件包是否有新的更新。

事实上,自 flutter 版本 1.12.1 起,该方法已被弃用。但直到版本 1.26.0-12.0.pre 之前,其仍然可用但不建议使用。 从那时起,它不再使用,这就解释了错误的原因。


在哪里可以找到它?之前:使用InheritFromWidgetOfExactType。 static Name of(BuildContext context) { return context.inheritFromWidgetOfExactType(Name); //之前 } - Daniele Angelini
@DanieleAngelini 在你的项目代码内部。我认为你的编译器会告诉你确切的位置。只需跟随它告诉你错误的地方,然后尝试重新调整代码。 - Kab Agouda
太好了!我花了几个小时找到这个确切的解决方案。 - user12961582
使用第一种解决方案降级,以运行此示例Flutter项目 -> https://github.com/2d-inc/developer_quest - Indra As Lesmana

12

Upd!

随着Flutter 2的发布,长期来看降级SDK并不是最好的解决方法。正确的做法是检查依赖项并将其更新,您可以使用flutter pub outdated终端命令来突出显示可用的更新 - 然后更改pubspec.yaml为较新的版本并尝试运行应用程序。可能会发生某些依赖关系没有更新或无法修复Flutter SDK中的破坏性更改的情况。在这种情况下,您将被锁定使用旧版Flutter(直到依赖关系得到更新或您用其他包替换它)。

Orig

运行flutter downgrade

在升级后收到相同的错误。从Flutter 1.26.0-12.0.pre•channel dev切换到先前的版本(1.26.0-8.0.pre)使构建再次正常工作。

如果您没有心情检查pubspec.yaml中的依赖项并将其更新为较新的版本(希望有null-safety的更新),则这是一个快速修复方法,但只是希望从上次离开的地方继续前进。最终,您将不得不检查所有软件包/插件并更新/替换为nullsafe版本。


啊,这个解决了,谢谢! - shawnblais

6
如果你需要在Flutter 1.26中运行provider,你应该运行:flutter downgrade。
如果你需要在Flutter 2.0.0 +中运行provider,你应该进行以下更改: pubspec.yaml
将 provider: ^2.0.1 更改为 ^5.0.0
main.dart
更改 builder 的创建方式
``` return MultiProvider( providers: [ ChangeNotifierProvider(create: (context) => YourProvider()), ], ); ```

谢谢,这对我有用。 - MEDANIS

5

对我来说是因为我使用了旧版本的依赖。请检查pub.dev是否有你的包的新版本!


1
https://github.com/flutter/flutter/issues/74519 - 与空安全相关的API出现了重大变化(删除了nullOk参数),通过查看他们更改了什么(Navigator,本地化)以及有多少插件依赖这些API,如果Flutter团队决定不撤销更改,可能需要几个月时间让插件开发人员跟上变化。 - Maxim Saplin

3

在与 Dev 和 master 通道一起使用时,经常会发生这种情况,Flutter 认为更稳定的是 Beta 通道的最佳月度选择。

flutter channel beta

Flutter构建版本发布渠道

我们通常会在每个月的第一个星期一从主分支中创建一个新的beta版本分支。这个分支包括Dart、引擎和框架。这个分支在接下来的几周内将被 "稳定化",这意味着我们将接受高影响问题的 cherry-pick 请求。当我们接近月底和下一个 beta 分支时,我们可能会减少我们愿意进行的 cherry-pick 数量。每季度,beta 分支将继续作为下一个稳定分支存在,具体细节请参阅下文。


昨天开始我在beta频道遇到了这个问题,不得不使用 flutter downgrade 命令降级。 - zgoda
最近从Beta切换到Master(或dev?)。重新切换到beta解决了问题,感谢Feras的解释! - Antonin GAVREL

0

这个问题发生在使用低于4.0.0版本的provider时。

你可以通过打开你的pubspec.yaml文件并将你的provider版本更改为4.0.0或更高版本来解决它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接