Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inheritFromWidgetOfExactType is deprecated use dependOnInheritedWidgetOfExactType instead

Tags:

flutter

dart

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!

like image 222
Jose Jet Avatar asked Dec 12 '19 12:12

Jose Jet


2 Answers

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

like image 190
Rémi Rousselet Avatar answered Nov 06 '22 07:11

Rémi Rousselet


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 (...)

like image 5
Kabirou Agouda Avatar answered Nov 06 '22 06:11

Kabirou Agouda