What is the solution of initializing things inside consumerWidget as because the initState method is not overridable here?
3 Answers
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
}
}

- 9,166
- 3
- 66
- 70
-
Yes, that could be a solution, but its completely overlooking the power of Riverpod / ConsumerWidget – towhid Oct 06 '20 at 15:08
-
I don’t think any other way as of now @towhid – Vinoth Vino Oct 06 '20 at 15:14
-
@VinothVino it seems ConsumerStateMixin is no longer available on flutter_riverpod 1.0.0 – Alexa289 Nov 21 '21 at 07:40
-
Riverpod 2.0 doesn't allow this anymore – JAgüero Dec 29 '22 at 19:40
-
1Great example just what I was looking for. And @parkorian this does work in Riverpod 2.3 . – Maka Mar 02 '23 at 23:42
-
3Important part of this answer to note is there is `read` in `initState` and `watch` in `build`. `Watch` doesn't work in `initState`. – BeniaminoBaggins Mar 04 '23 at 05:35
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.

- 5,110
- 2
- 19
- 36
-
3After 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
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.

- 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