StateNotifierProvider.overrideWithState #1195
scopendo
started this conversation in
Show and tell
Replies: 2 comments
-
Hey! Thanks for this. What is Thanks:) |
Beta Was this translation helpful? Give feedback.
0 replies
-
Ah, that wasn't meant to be in there :) It's just a helper function to create a provider that is always intended to be overridden and so throws an exception in its create function. /// A [Provider] whose value must be set at startup by overriding the
/// value on [ProviderScope].
///
/// Although this is a function, we name it like a class using Pascal-case.
///
/// ignore: non_constant_identifier_names
Provider<T> MustOverrideProvider<T>() {
return Provider<T>(
(_) => throw ProviderNotOverriddenException(),
);
}
class ProviderNotOverriddenException implements Exception {
@override
String toString() {
return '''
The value for this provider must be set by an override on ProviderScope
at runtime, usually when the application is first started.''';
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm using the following approach to override the value of a
StateNotifier
for situations where I need to couple app logic with the overridden state. ThisoverrideWithState
method uses an internal provider to capture the new state value as a dependency that can be watched in the overridden state notifier provider's create function.Usage:
Beta Was this translation helpful? Give feedback.
All reactions