19

What is the solution of initializing things inside consumerWidget as because the initState method is not overridable here?

Delgan
  • 18,571
  • 11
  • 90
  • 141
towhid
  • 2,778
  • 5
  • 18
  • 28

3 Answers3

43

Riverpod v2.3.6

You can use ConsumerStatefulWidget and ConsumerState

final helloWorldProvider = Provider((_) => 'Hello world');

class RiverpodExample extends ConsumerStatefulWidget {
  const RiverpodExample({super.key});

  @override
  ConsumerState<RiverpodExample> createState() => _RiverpodExampleState();
}

class _RiverpodExampleState extends ConsumerState<RiverpodExample> {
  @override
  void initState() {
    super.initState();

    final value = ref.read(helloWorldProvider);
    print(value); // Hello world
  }

  @override
  Widget build(BuildContext context) {
    final value = ref.watch(helloWorldProvider);

    return Text(value); // Hello world
  }
}
Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70
5

I'm not totally sure how to answer your question as I have not worked with ConsumerWidget. I'd presume the idea is to keep most of your state in providers.

However, I would like to recommend using hooks_riverpod alongside flutter_hooks (same developer).

This makes keeping state local to the widget simple and also provides easy access to providers.

For example:

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

  @override
  Widget build(BuildContext context) {
    final test = useProvider(Test.provider());

    final controller = useTextEditingController();
    final loading = useState(false);
    final buttonText = useState('Change me!');

    return Column(
      children: [
        TextField(controller: controller),
        if (!loading) RaisedButton(
          onPressed: () async {
            loading.value = true;
            await Future.delayed(const Duration(seconds: 1));
            buttonText.value = controller.text;
            loading.value = false;
          }
          child: Text(buttonText.value),
        ),
        if (loading) const CircularProgressIndicator(),
        // Do something with providers, etc.
      ],
    ),
  );
}

Just a quick example, but there are plenty of resources (flutter_hooks, hooks_riverpod) to help you along. Also, check out examples from the developer on riverpod hooks usage.

Alex Hartford
  • 5,110
  • 2
  • 19
  • 36
  • 3
    After writing my code guided by your answer, I can conclude that this is the correct way to implement flutter_hooks with hook_riverpod and set variables at widget start up (and it's in the documentation). It's 'cause the initial values don't overwrite what you could set or update in the returned widget even though you rebuild it, so it's exactly like using initState. – José Luna Dec 06 '20 at 08:33
0

I may be late but with the upcoming 1.0.0 of Riverpod you will be able to use https://pub.dev/documentation/flutter_riverpod/1.0.0-dev.2/flutter_riverpod/ConsumerStatefulWidget-class.html and https://pub.dev/documentation/flutter_riverpod/1.0.0-dev.2/flutter_riverpod/ConsumerState-class.html, which does exactly what you want.

wujek
  • 10,112
  • 12
  • 52
  • 88
  • @wujek, the accepted answer by Vinoth did the trick for me. Try to follow it and see if you can come right even without the links. I think they are still updating the docs. – Robert Mrobo Aug 30 '21 at 21:38
  • What's funny is that his links are just as broken as are mine, but at least he has inline code ;) – wujek Aug 31 '21 at 07:18