AlbertJanP
AlbertJanP

Reputation: 155

ancestorStateOfType is deprecated use findAncestorStateOfType instead

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?

Upvotes: 15

Views: 11759

Answers (1)

Benjamin
Benjamin

Reputation: 6161

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

Upvotes: 30

Related Questions