首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:未为类“inheritFromWidgetOfExactType”定义方法“BuildContext”

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

Stack Overflow用户
提问于 2021-01-16 12:41:32
回答 5查看 37.4K关注 0票数 37

今天,当我编译我的颤振项目时,它显示了以下错误:

代码语言:javascript
运行
复制
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)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

我用的是颤振主通道,这是颤振医生:

代码语言:javascript
运行
复制
[✓] 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)

升级到颤振2.0.1后,再次显示此错误:

代码语言:javascript
运行
复制
~/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)
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2021-02-07 20:02:10

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

这种方法是不可取的。使用dependOnInheritedWidgetOfExactType代替。

您的项目只包含它

第一种解决办法:

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

代码语言:javascript
运行
复制
flutter downgrade

第二种解决办法:

切换到beta通道

代码语言:javascript
运行
复制
flutter channel beta

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

第三种解决办法:

更新您的项目

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

替换的示例

以前:InheritFromWidgetOfExactType

代码语言:javascript
运行
复制
static Name of(BuildContext context) {
  return context.inheritFromWidgetOfExactType(Name);  //before
}

Now with dependOnInheritedWidgetOfExactType (重组)

代码语言:javascript
运行
复制
static Name of(BuildContext context) {
  return context.dependOnInheritedWidgetOfExactType<Name>();  //after
}

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

错误也可能是您导入的包造成的:

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

实际上,这种方法自颤振1.12.1版起就贬值了。但在此之前,它的使用是可能的,但不建议使用。因为版本1.26.0-12.0,它不再被使用,这解释了错误。

票数 48
EN

Stack Overflow用户

发布于 2021-01-23 08:23:30

Upd!

随着颤振2的发布,降级SDK并不是最好的解决办法,从长远来看。正确的做法是通过依赖关系并更新它们,您可以使用flutter pub outdated终端命令突出显示可用的更新,然后用更新的版本更改pubspec.yaml并尝试运行您的应用程序。可能会发生这样的情况:某些依赖项将不会对Flutter中的最新更改进行更新或修复。在本例中,您将被锁定为使用旧的Flutter (直到更新依赖关系或用其他包替换它)。

Orig

运行flutter downgrade

升级后立即收到相同的错误。从颤振1.26.0到12.0. the·channel dev切换到以前的verion (1.26.0-8.0),使构建工作再次开始。

这是一个快速修复,以防您没有心情通过pubspec.yaml中的依赖项,将它们更新到新的依赖项(希望有对空安全的更新),而只想从您离开的地方继续进行。最后,您必须检查所有的包/插件,并用空安全版本更新/替换它们。

票数 11
EN

Stack Overflow用户

发布于 2021-01-28 19:40:34

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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65749767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档