【问题标题】:How to fix Error: The method 'ancestorWidgetOfExactType' isn't defined for the class 'BuildContext'如何修复错误:没有为类“BuildContext”定义方法“ancestorWidgetOfExactType”
【发布时间】:2021-06-19 02:29:53
【问题描述】:

为了解决这个问题,我进行了搜索,但我发现没有为“BuildContext”类问题的解决方案定义“inheritFromWidgetOfExactType”的解决方案。我找不到任何解决这个问题的方法 'ancestorWidgetOfExactType' 方法没有为类 'BuildContext' 定义。

升级flutter 2.0.2后编译项目后出现这些错误

/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/pull_to_refresh-1.6.2/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' ('/C:/src/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);
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/pull_to_refresh-1.6.2/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' ('/C:/src/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>());
                    ^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/pull_to_refresh-1.6.2/lib/src/smart_refresher.dart:988:20: Error: The method 'inheritFromWidgetOfExactType' isn't defined for the class 'BuildContext'.
 - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('/C:/src/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);
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_staggered_animations-0.1.2/lib/src/animation_configuration.dart:161:20: Error: The method 'ancestorWidgetOfExactType' isn't defined for the class 'BuildContext'.
 - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('/C:/src/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(AnimationConfiguration)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_staggered_animations-0.1.2/lib/src/animation_limiter.dart:75:20: Error: The method 'ancestorWidgetOfExactType' isn't defined for the class 'BuildContext'.
 - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('/C:/src/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(_AnimationLimiterProvider)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_neumorphic-3.0.2/lib/src/widget/app_bar.dart:147:57: Error: No named parameter with the name 'nullOk'.
    final ScaffoldState scaffold = Scaffold.of(context, nullOk: true);
                                                        ^^^^^^
/C:/src/flutter/packages/flutter/lib/src/material/scaffold.dart:1944:24: Context: Found this candidate, but the arguments don't match.
  static ScaffoldState of(BuildContext context) {
                       ^^
2

FAILURE: Build failed with an exception.

* Where:
Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 991

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 5m 31s
Exception: Gradle task assembleDebug failed with exit code 1

这是我的 Flutter 医生

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.0.2, on Microsoft Windows [Version 10.0.19042.867], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[√] Android Studio (version 4.1.0)
[√] VS Code (version 1.54.2)
[√] Connected device (2 available)

【问题讨论】:

  • 执行flutter pub clean 并重新运行。

标签: flutter


【解决方案1】:

在 pubspec.yaml 中尝试:
pull_to_refresh: 1.6.4-nullsafety.0

【讨论】:

    【解决方案2】:

    inheritFromWidgetOfExactTypeancestorWidgetOfExactType 从 Flutter 框架中移除,尝试 dependOnInheritedWidgetOfExactType()findAncestorWidgetOfExactType() p>

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 2021-04-15
      • 2021-09-01
      • 2021-09-17
      • 1970-01-01
      • 2021-02-11
      • 2021-02-24
      • 2020-05-21
      • 2019-05-10
      相关资源
      最近更新 更多