gpt4 book ai didi

flutter - 如何只从 InheritedWidget 调用方法一次?

转载 作者:IT老高 更新时间:2023-10-28 12:38:02 27 4
gpt4 key购买 nike

我正在使用 Rx_command、Rx_dart 使用响应式组件流/可观察对象进行开发

问题:

  • 在我的 Flutter 应用中,我继承了可以在任何地方调用的小部件:

    FooProvider.of(context).foo.method1...

  • 我需要在 UI 首次加载时第一次调用该方法

  • 我不能使用 init.state,因为这是不可能的
  • 我使用 didchangedependencies 它可以工作,但是..

    ...每次ui重新加载时,都会调用didchangedependencies并再次执行该方法。

我不希望它被执行,我不能使用 init.state

该方法如何只执行一次

最佳答案

使用 context.ancestorInheritedElementForWidgetOfExactType

代替 context.inheritFromWidgetOfExactType
final myInherited = context.ancestorInheritedElementForWidgetOfExactType(MyInherited)?.widget;

此方法在 initState

中可用

关于flutter - 如何只从 InheritedWidget 调用方法一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53022964/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com