【发布时间】: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)。但不幸的是,其他东西在开发频道上不起作用。