错误:未为类“BuildContext”定义方法“inheritFromWidgetOfExactType”

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

提问人:Dolphin 提问时间:1/16/2021 最后编辑:Dolphin 更新时间:12/16/2022 访问量:40981

问:

今天当我编译我的 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)
扑动

评论

0赞 user1032620 1/18/2021
从测试版切换到主频道后遇到同样的问题。如果您找到解决方案,请告诉我。
0赞 Justin Klaus 1/18/2021
几天前升级 flutter 后遇到同样的问题。我通过升级提供程序包在某种程度上修复了它,但现在我遇到了新的问题。
2赞 Dolphin 1/18/2021
我现在切换到稳定频道,也许在升级后会遇到错误。@user1032620
0赞 user1032620 1/19/2021
不幸的是,两者都没有解决我的情况。内置测试版没有问题,但需要主通道才能避免应用程序最终在测试飞行中崩溃。
0赞 Shvet 1/20/2021
有人找到解决方案了吗?

答:

12赞 Maxim Saplin 1/23/2021 #1

更新!

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

奥里格

flutter downgrade

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

这是一个快速修复程序,以防您没有心情在将它们更新到较新的依赖项时通过依赖项(希望有 null-safety 的更新),而只是想从您离开的地方继续。最终,您必须遍历所有软件包/插件,并用 nullsafe 版本更新/替换它们。pubspec.yaml

评论

0赞 shawnblais 1/27/2021
啊。这解决了它,ty!
5赞 murilopbs 1/29/2021 #2

对我来说,是因为我使用的是旧版本的依赖项。检查 pub.dev,看看是否有新版本的软件包!

评论

1赞 Maxim Saplin 1/31/2021
github.com/flutter/flutter/issues/74519 - API 中有一个与 null-safety 相关的重大更改(删除 nullOk 参数),通过浏览它们更改的内容(Navigator、Localisation)以及有多少插件依赖于这些 API,插件开发人员可能需要几个月的时间才能赶上更改(如果 Flutter 团队决定不恢复更改)
3赞 0xFK 2/3/2021 #3

这在使用 Dev 和 master 频道时经常发生,flutter 认为更稳定的最佳每月主频道选择是频道 Beta

flutter channel beta

Flutter 构建发布通道

我们将在 master 开始时分支到一个新的 beta 版本 月份,通常是第一个星期一。这将包括一个分支 Dart、引擎和框架。然后,此分支将是 在接下来的几周内“稳定”,这意味着我们将接受 对高影响问题的 cherrypick 请求。当我们接近尾声时 月和下一个 beta 分支,我们可能会减少 我们愿意做的樱桃。每季度一次,beta 分支将 继续生活,成为下一个稳定的分支,详见下文。

评论

0赞 zgoda 2/5/2021
从昨天开始,我就在测试版频道上面临这个问题,无论如何都不得不这样做。flutter downgrade
0赞 Antonin GAVREL 2/7/2021
最近从 Beta 切换到 Master(或开发?重新切换到测试版解决了这个问题,感谢 Feras 的解释!
49赞 Kab Agouda 2/8/2021 #4

此错误是由 inheritFromWidgetOfExactType 方法引起的。

此方法已弃用。请改用 dependOnInheritedWidgetOfExactType

您的项目仅包含它

第一个解决方案:

手动返回版本或使用以下命令:1.26.0-8.0

flutter downgrade

第二种解决方案:

切换到 Beta 版频道

flutter channel beta

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

第三种解决方案:

更新项目

在代码中找到 inheritFromWidgetOfExactType 方法,并使用 dependOnInheritedWidgetOfExactType 对其进行调整。

替换示例

之前:使用 InheritFromWidgetOfExactType

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

现在使用 dependOnInheritedWidgetOfExactType (Rerecommended)

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

现在,该方法不是以 a 作为参数,而是泛型的。
简要而不是
Type<...>()(...)

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

溶液:检查此程序包是否有新的更新

事实上,这种方法从 flutter 的 1.12.1 版本开始就被弃用了。但在此之前,它的使用是可能的,但不推荐使用。 从版本 1.26.0-12.0.pre 开始,它不再使用,这解释了错误。

评论

0赞 Daniele Angelini 3/24/2021
在哪里可以找到它?Before : with InheritFromWidgetOfExactType static Name of(BuildContext context) { return context.inheritFromWidgetOfExactType(Name); //之前 }
0赞 Kab Agouda 3/25/2021
@DanieleAngelini 在项目的代码中。我想你的编译器会告诉你确切的位置。只需按照它告诉您错误的地方进行操作,然后尝试重新调整代码即可。
0赞 4/3/2021
伟大!确切的解决方案,我从几个小时内找到了
0赞 Indra As Lesmana 5/2/2022
与第一个解决方案降级合作,运行这个示例 flutter 项目 -> github.com/2d-inc/developer_quest
6赞 Jeisson Santacrus 4/23/2021 #5

如果你需要在 flutter 1.26 中运行 provider,你应该运行:flutter downgrade。

如果你需要在 flutter 2.0.0 + 中运行提供程序,你应该改变:

pubspec.yaml

提供程序:^ 2.0.1 到 ^5.0.0

main.dart

用于创建的更改生成器

return MultiProvider( providers: [ ChangeNotifierProvider(create: (context) => YourProvider()), ],

评论

0赞 MEDANIS 10/29/2021
谢谢,这对我有用
0赞 habeshacoder 12/7/2022 #6

使用低于 4.0.0 的版本时,会出现此问题。provider

您可以通过打开文件并将提供程序版本更改为 4.0.0 或更高版本来解决此问题。pubspec.yaml