【问题标题】:How to call a method from InheritedWidget only once?如何只从 InheritedWidget 调用方法一次?
【发布时间】:2019-04-01 01:18:44
【问题描述】:

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

问题:

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

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

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

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

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

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

方法如何只执行一次

【问题讨论】:

    标签: flutter rxdart


    【解决方案1】:

    context.ancestorInheritedElementForWidgetOfExactType代替context.inheritFromWidgetOfExactType

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

    此方法在initState内部可用

    【讨论】:

      猜你喜欢
      • 2014-06-07
      • 2019-09-24
      • 2011-09-06
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 2023-03-16
      相关资源
      最近更新 更多