Jose Jet
Jose Jet

Reputation: 1590

inheritFromWidgetOfExactType is deprecated use dependOnInheritedWidgetOfExactType instead

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!

Upvotes: 55

Views: 32517

Answers (2)

Kab Agouda
Kab Agouda

Reputation: 7269

InheritFromWidgetOfExactType method is deprecated , Use dependOnInheritedWidgetOfExactType method instead.

Example of a replacement:

Before : with InheritFromWidgetOfExactType

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

Now with dependOnInheritedWidgetOfExactType (Recommanded)

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

Now instead of taking a Type as argument, The method is generic .
Brief <...>() instead of (...)

Upvotes: 6

R&#233;mi Rousselet
R&#233;mi Rousselet

Reputation: 277057

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

Upvotes: 190

Related Questions