flutter inheritFromWidgetOfExactType已弃用,请改用dependOnInheritedWidgetOfExactType

xwmevbvl  于 2023-04-07  发布在  Flutter
关注(0)|答案(2)|浏览(171)

自Flutter 1.12发布以来,我的以下代码:

static MyInheritedWidget of(BuildContext context) {
  return context.inheritFromWidgetOfExactType(MyInheritedWidget) as MyInheritedWidget;
}

警告如下:
“inheritFromWidgetOfExactType”已弃用,不应使用。请改用dependOnInheritedWidgetOfExactType。此功能在v1.12.1之后已弃用。请尝试使用替换项替换已弃用的成员。
但是当我试图替换它时,它不起作用:

static MyInheritedWidget of(BuildContext context) {
  return context.dependOnInheritedWidgetOfExactType(MyInheritedWidget) as MyInheritedWidget;
}

有人知道怎么做吗?谢谢!

rqdpfwrv

rqdpfwrv1#

API略有变化。
现在,该方法不再使用Type作为参数,而是泛型的。
之前:

final widget = context.inheritFromWidgetOfExactType(MyInheritedWidget) as MyInheritedWidget;

之后:

final widget = context.dependOnInheritedWidgetOfExactType<MyInheritedWidget>();

请注意,造型不再需要了

oxf4rvwz

oxf4rvwz2#

InheritFromWidgetOfExactType方法已弃用,请改用dependOnInheritedWidgetOfExactType方法。
替换示例
**之前:**带InheritFromWidgetOfExactType

static Name of(BuildContext context) {
  return context.inheritFromWidgetOfExactType(Name);  //here
}

现在使用dependOnInheritedWidgetOfExactType(推荐)

static Name of(BuildContext context) {
  return context.dependOnInheritedWidgetOfExactType<Name>();  //here
}

现在不再使用Type作为参数,该方法是泛型的。
简要介绍<...>()而不是(...)

相关问题