【问题标题】:Pass and update State Value from other function Flutter Hook Widget从其他功能 Flutter Hook Widget 传递和更新状态值
【发布时间】:2021-04-24 10:17:38
【问题描述】:

我是 Flutter hooks 的新手,我有要求我必须使用 HookWidget 而不是 StatefulWidget。据我所知,useState 只能在构建函数中声明。

Widget build(BuildContext context) {
    final selectedBook = useState("");
    return Container(
       child: _buildBookListContainer(context)
    )  
}

Widget _buildBookListContainer(BuildContext context) {
  //I will want to update the state value or read the state value in the child function
  //how do I do that?
  //Example: selectedBook.value = "xxx";
}

我尝试将状态值作为函数参数传递,但它不起作用。我可以知道是不是所有的 HookWidget 类都只会在构建函数中编写所有组件而不进行重构?

【问题讨论】:

  • 您好,您对此有什么发现吗?

标签: flutter flutter-hooks


【解决方案1】:

假设您使用的是 Riverpod,请在 https://www.youtube.com/watch?v=0lPzFS5CAPs 观看我相对较短的“使用我的 Riverpod 入门套件从头开始构建计数器应用程序”。它展示了如何创建一个 StateProvider,然后引用它(使用监听)或更新它(不监听)以精确控制小部件的重建。

【讨论】:

    猜你喜欢
    • 2020-04-29
    • 2020-05-07
    • 2023-04-09
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2019-10-09
    • 2018-07-06
    相关资源
    最近更新 更多