inheritFromWidgetOfExactType is deprecated use dependOnInheritedWidgetOfExactType instead

Question

Asked by Jose J on December 12, 2019 (source).

Since the release of Flutter 1.12 my following code:

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

warns with the following:

'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!

Answer

Question answered by Rémi R (source).

The API changed slightly.

Now instead of taking a Type as argument, the method is generic.

Before:

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

After:

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

Note that the cast is no longer necessary

DART FLUTTER
SHARE: