【问题标题】:Error: The method 'ancestorStateOfType' isn't defined for the class 'BuildContext'错误:没有为类“BuildContext”定义方法“ancestorStateOfType”
【发布时间】:2021-04-15 20:32:39
【问题描述】:

如下使用类 Bloc 提供程序时,出现错误:

'没有为类型'BuildContext'定义方法'ancestorInheritedElementForWidgetOfExactType'。'

所以我把这行换成了context.ancestorInheritedElementForWidgetOfExactType(type)?.widget;

用这行context.getElementForInheritedWidgetOfExactType<_BlocProviderInherited<T>>().widget;

然后我得到以下错误:

Error output from Xcode build:
↳
** BUILD FAILED **

Xcode's output:
↳
../../../development/flutter/.pub-cache/hosted/pub.dartlang.org/dynamic_theme-1.0.1/lib/dynamic_theme.dart:25:20: Error: The method 'ancestorStateOfType' isn't defined for the class 'BuildContext'.
 - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('../../../development/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(const TypeMatcher<DynamicThemeState>());
                   ^^^^^^^^^^^^^^^^^^^

Command PhaseScriptExecution failed with a nonzero exit code
note: Using new build system
note: Building targets in parallel
note: Planning build
note: Constructing build description

Could not build the precompiled application for the device.

Error launching application on iPhone 

这是我正在使用的 Bloc Provider:

class BlocProvider<T extends BlocBase> extends StatefulWidget {
   BlocProvider({
   Key key,
   @required this.child,
   @required this.bloc,}) : super(key: key);
   final Widget child;
   final T bloc;
   @override
   _BlocProviderState<T> createState() => _BlocProviderState<T>();

   static T of<T extends BlocBase>(BuildContext context) {
   final type = _typeOf<_BlocProviderInherited<T>>();
   _BlocProviderInherited<T> provider =
   context.ancestorInheritedElementForWidgetOfExactType(type)?.widget;
   return provider?.bloc;
  }
}

我在主频道,Flutter(频道主,1.26.0-2.0.pre.275)

【问题讨论】:

  • 如果我没记错的话,你的初始错误意味着它没有在上游小部件树中找到 bloc 实例
  • 在 Flutter 开发频道上它正在工作。 Flutter(频道开发,1.26.0-1.0.pre)。但不幸的是,其他东西在开发频道上不起作用。

标签: flutter bloc


【解决方案1】:

尝试更改 pubspec.yaml 中的软件包版本,它对我有用 我将提供程序版本更改为 5.0.0 ,然后它开始工作

【讨论】:

  • 请在您的答案中添加一些代码 sn-ps 以便更清楚。
【解决方案2】:

改成

context.findAncestorStateOfType&lt;DynamicThemeState&gt;();

context.findAncestorStateOfType&lt;*Your_Library*&gt;();

这会起作用

【讨论】:

    【解决方案3】:

    此错误是由于包过时引起的。因此,升级软件包是一种解决方案。但有时,即使升级包也不起作用,因为包的创建者可能没有升级它或已经放弃它。在这种情况下切换到其他包。

    在这种特殊情况下,升级 flutter_bloc 可能会起作用。

    GradientAppBar 遇到的另一个类似问题。它给出了与上面相同的错误。解决方案是使用 AppBar,因为 AppBar 现在支持内部渐变。

    AppBar(
          flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              Colors.amber[700],
              Colors.grey[800],
            ]
          )
        )
      ),
    

    我在这里发布关于渐变应用栏的帖子,因为我在这篇文章中找到了关于 GradientAppBar 的解决方案但找不到。

    【讨论】:

      【解决方案4】:

      只要改成这样就可以了。

      context.findAncestorStateOfType&lt;DynamicThemeState&gt;();

      只是 Flutter 新版本的代码更新,旧版本已被贬值

      【讨论】:

      • 请在您的回答中添加一些解释
      • 这只是新flutter版本中的代码更新,旧版本已贬值。
      • 所以将它添加到您的 aswer 中
      【解决方案5】:

      你使用的是旧版本的flutter_bloc,首先你应该升级它。

      但是,实际上真正的问题是 context.ancestorInheritedElementForWidgetOfExactType(type) 已被弃用,取而代之的是 context.getElementForInheritedWidgetOfExactType&lt;T&gt;() 但在稳定频道中仍然可用,但在 master 中已被删除。

      那么有必要更新你的代码,因为 Bloc 发生了重大变化。

      更新:提到的更改已经在稳定频道中。

      【讨论】:

      • 我确实将 flutter_bloc 更新为 ^6.1.1 并尝试了预发布 ^7.0.0-nullsafety.0,但它们都没有在主频道上工作。 bloc_provider 有新的实现吗?对我来说,BuildContext 类中似乎没有“ancestorStateOfType”方法。
      • context.ancestorStateOfType() 也已弃用
      【解决方案6】:

      升级开发频道后几乎是同样的问题

      .pub-cache/hosted/pub.dartlang.org/dynamic_theme-1.0.1/lib/dynamic_theme.dart:25:20: Error: The method 'ancestorStateOfType' isn't defined for the class 'BuildContext'.
      'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('/home/liudmila/snap/flutter/common/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'.
      

      使用块 4.0.0

      只需在文件 .pub-cache/../dynamic_theme.dart:25:20 中更改它

      context.ancestorStateOfType()
      

      关于那个

      context.findAncestorStateOfType<DynamicThemeState>();
      

      这是临时解决方案,但应用程序可以工作

      【讨论】:

      • 我能够修复我的项目
      猜你喜欢
      • 2021-07-26
      • 2021-06-19
      • 2021-09-01
      • 2021-09-17
      • 1970-01-01
      • 2021-02-11
      • 2021-02-24
      • 2020-05-21
      • 2019-05-10
      相关资源
      最近更新 更多