How to make a run-time error when using a Scoped Provider that is not overridden? #3667
-
I want to create a Scoped Provider that returns a Notifier using code generation. I want this Scoped Provider to make a runtime error if it is not overridden by a ProviderScope, but how should I achieve this? I have created the following Notifier. The initial value should always be 0. @Riverpod(dependencies: [])
class CountNotifier extends _$CountNotifier {
@override
int build() {
return 0;
}
void increment() {
state = state + 1;
}
} It can be treated as a Scoped Provider by using ProviderScope as follows. ProviderScope(
overrides: [
countNotifierProvider.overrideWith(CountNotifier.new),
],
child: MyScreen._(keyword: keyword),
) However, when I forget to override it, it works, not a runtime error How can I make it a runtime error when I don't override the Scoped Provider? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can enforce this by overwriting in the root providerscope as follows ProviderScope(
overrides: [
countNotifierProvider.overrideWith(() => throw UnimplementedError()),
],
); |
Beta Was this translation helpful? Give feedback.
You can enforce this by overwriting in the root providerscope as follows