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

mid*_*007 16 flutter bloc

如下使用类 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 
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的 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;
  }
}
Run Code Online (Sandbox Code Playgroud)

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

小智 19

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

.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'.
Run Code Online (Sandbox Code Playgroud)

与块 4.0.0

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

context.ancestorStateOfType()
Run Code Online (Sandbox Code Playgroud)

在那

context.findAncestorStateOfType<DynamicThemeState>();
Run Code Online (Sandbox Code Playgroud)

那是临时解决方案,但应用程序有效


Fra*_*eno 3

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

但是,实际上真正的问题是它context.ancestorInheritedElementForWidgetOfExactType(type)已被弃用context.getElementForInheritedWidgetOfExactType<T>(),但在稳定通道中仍然可用,但在 master 中已被删除。

然后有必要更新您的代码,因为 Bloc 发生了重大更改。

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