2

I am new to Flutter hooks and I have requirements that I have to use HookWidget instead of StatefulWidget. As I know, useState can only be declared within the build function.

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";
}

I tried passing down the state value as function arguments but it will not work. May I know that is it all HookWidget class will just write all components inside the build function without refractoring?

Avan CaiJun
  • 111
  • 1
  • 1
  • 11

1 Answers1

0

Presuming you're using Riverpod, watch my relatively short "building the counter app from scratch with my riverpod starter kit" at https://www.youtube.com/watch?v=0lPzFS5CAPs. It shows how to create a StateProvider, and then either reference it (with listen) or update it (no listen) for precise control of widget rebuilds.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70