我是新手,在弹出此错误时,我正在关注教程。 ancestorRenderObjectOfType已弃用,由findAncestorRenderObjectOfType取代,因此dart向我抛出错误。
导师在他的旧 Dart 视频中做了什么:
static T of<T extends BlocBase>(BuildContext context) {
final type = _typeOf<BlocProvider<T>>();
BlocProvider<T> provider = context
.context.ancestorRenderObjectOfType(type);
return provider.bloc;
}静态类型_typeOf()=> T;
}
我在代码中做了什么
static T of<T extends BlocBase>(BuildContext context) {
final type = _typeOf<BlocProvider<T>>();
BlocProvider<T> provider = context
.findAncestorRenderObjectOfType();
return provider.bloc;
}
static Type _typeOf<T>() => T;
}
如果我放BlocProvider<T> provider = context
.findAncestorRenderObjectOfType(type);
我说错了太多的立场争论。预期0,但找到1。
整个代码
abstract class BlocBase {
void dispose();
}
//Genric Bloc provider
class BlocProvider<T extends BlocBase> extends StatefulWidget {
BlocProvider({
Key key,
@required this.child,
@required this.bloc,
}) : super(key: key);
final T bloc;
final Widget child;
@override
_BlocProviderState<T> createState() => _BlocProviderState<T>();
static T of<T extends BlocBase>(BuildContext context) {
final type = _typeOf<BlocProvider<T>>();
BlocProvider<T> provider = context
.findAncestorRenderObjectOfType(); //context.ancestorRenderObjectOfType(type); GOTCHA
return provider.bloc;
}
static Type _typeOf<T>() => T;
}
class _BlocProviderState<T> extends State<BlocProvider<BlocBase>> {
@override
void dispose() {
widget.bloc.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return widget.child;
}
}
谢谢! 最佳答案
findAncestorRenderObjectOfType
不以type
作为参数,而是一种通用方法,您可以在调用该方法时提供类型。因此,您的代码将如下所示:
static T of<T>(BuildContext context) {
BlocProvider<T> provider = context
.findAncestorRenderObjectOfType<BlocProvider<T>>();
return provider.bloc;
}
关于flutter - 未使用局部变量的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63013613/