ancestorStateOfType is deprecated use findAncestorStateOfType instead

Question

Asked by AlbertJanP on December 22, 2019 (source).

Since the release of Flutter 1.12 my following code for restarting the app:

final MyAppState state = context.ancestorStateOfType(const TypeMatcher<MyAppState>()):

warns with the following:

'ancestorStateOfType' is deprecated and shouldn't be used. Use findAncestorStateOfType instead. This feature was deprecated after v1.12.1.. Try replacing the use of the deprecated member with the replacement.

and with

'TypeMatcher' is deprecated and shouldn't be used. TypeMatcher has been deprecated because it is no longer used in framework(only in deprecated methods). This feature was deprecated after v1.12.1.. Try replacing the use of the deprecated member with the replacement.

For some reason I can't seem to figure out how to refactor this code to get it working. Anyone to the rescue?

Answer

Question answered by Benjamin (source).

All you have to do is just put the widget that extends State<StatefulWidget> so for your example, since the class is called MyAppState, I'm assuming it extends State<MyApp> so, therefore:

final MyAppState state = context.findAncestorStateOfType<MyAppState>();
DART FLUTTER
SHARE: