1

There quote is from Riverpod documentation:

A StatefulWidget that can use Hook

It's usage is very similar to StatefulWidget, but use hooks inside State.build.

The difference is that it can use Hook, which allows HookWidget to store mutable data without implementing a State.

besides this, I can't find any sample code or another tutorial or any description more than the quote.

in this simple HookWidget how can I implement that with StatefulHookWidget?

class MyHomePage extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final store = useMemoized(() => MyStore());
    return Container();
  }
}
Benyamin
  • 1,008
  • 11
  • 18
DolDurma
  • 15,753
  • 51
  • 198
  • 377

2 Answers2

2

A StatefulHookWidget is for when you need to use any of the overridable functions of a regular StatefulWidget - like didChangeDependencies, or initState, or dispose.

Generally, unless you have a really good or niche reason to use StatefulHookWidget, do prefer HookWidget.

Essentially, if we replicate your example, both Class1 and Class2 are equivalent in terms of the end product. The only difference is the verbiage needed to get there.

class Class1 extends HookWidget {
  const Class1({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final store = useMemoized(() => MyStore());
    return Container();
  }
}

class Class2 extends StatefulHookWidget {
  const Class2({Key? key}) : super(key: key);

  @override
  _Class2State createState() => _Class2State();
}

class _Class2State extends State<Class2> {
  @override
  Widget build(BuildContext context) {
    final store = useMemoized(() => MyStore());
    return Container();
  }
}
Agon Noga
  • 563
  • 1
  • 9
  • 17
0

I am on the latest version of riverpod so I am using StatefulHookConsumerWidget Following is one way to implement StatefulHookConsumerWidget. This might give you a hint for your answer

class MyHomePage extends StatefulHookConsumerWidget {
  @override
  _MyHomePage State createState() => _SocietyHomeState();
}

class MyHomePage extends ConsumerState<SocietyHome> {
  Widget build(BuildContext context) {
    return Container();
  }
}
Sai
  • 19
  • 1
  • 3