ancestorStateOfType已过时,请改用findAncestorStateOfType

时间:2019-12-22 21:18:26

标签: flutter dart

自Flutter 1.12发布以来,我的以下代码用于重新启动应用程序:

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

警告以下内容:

  

'ancestorStateOfType'已弃用,不应使用。请改用findAncestorStateOfType。 v1.12.1之后不推荐使用此功能。请尝试使用替换成员替换不推荐使用的成员。

  

'TypeMatcher'已被弃用,不应使用。 TypeMatcher已被弃用,因为它不再在框架中使用(仅在不建议使用的方法中使用)。 v1.12.1之后不推荐使用此功能。请尝试使用替换成员替换不推荐使用的成员。

由于某种原因,我似乎无法弄清楚如何重构此代码以使其正常工作。有人来救援吗?

1 个答案:

答案 0 :(得分:1)

您所要做的就是放扩展State<StatefulWidget>的小部件,因此,在您的示例中,由于该类名为MyAppState,所以我假设它扩展了State<MyApp>,因此:

final MyAppState state = context.findAncestorStateOfType<State<MyApp>>();