不推荐使用InheritedFromWidgetOfExactType,而是使用dependOnOnInheritedWidgetOfExactType [英] inheritFromWidgetOfExactType is deprecated use dependOnInheritedWidgetOfExactType instead
问题描述
自Flutter 1.12发布以来,我的以下代码:
Since the release of Flutter 1.12 my following code:
static MyInheritedWidget of(BuildContext context) {
return context.inheritFromWidgetOfExactType(MyInheritedWidget) as MyInheritedWidget;
}
警告以下内容:
'inheritFromWidgetOfExactType'已弃用,不应使用.请改用DependOnInheritedWidgetOfExactType.v1.12.1之后不推荐使用此功能.尝试用替代品替换不赞成使用的成员.
'inheritFromWidgetOfExactType' is deprecated and shouldn't be used. Use dependOnInheritedWidgetOfExactType instead. This feature was deprecated after v1.12.1.. Try replacing the use of the deprecated member with the replacement.
但是当我尝试更换它时,它不起作用:
But when I try to replace it, it does not work:
static MyInheritedWidget of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType(MyInheritedWidget) as MyInheritedWidget;
}
有人知道该怎么做吗?谢谢!
Does someone know how to do it? Thanks!
推荐答案
API稍有变化.
现在,该方法不再使用 Type
作为参数,而是通用的.
Now instead of taking a Type
as argument, the method is generic.
之前:
final widget = context.inheritFromWidgetOfExactType(MyInheritedWidget) as MyInheritedWidget;
之后:
final widget = context.dependOnInheritedWidgetOfExactType<MyInheritedWidget>();
请注意,不再需要强制转换
Note that the cast is no longer necessary
这篇关于不推荐使用InheritedFromWidgetOfExactType,而是使用dependOnOnInheritedWidgetOfExactType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!