I have been using provider as state management and had newly migrated to riverpod. Riverpod has a lot of providers which is confusing for me. I am using NotifierProvider and have issue calling a function
Here is the screen and the function. I wanted to access the update function but i can only access the model variables. Error says “The getter ‘notifier’ isn’t defined for the type ‘SignupModel'”
class SignUpScreen extends ConsumerStatefulWidget {
static const routeName = Screens.signUpScreen;
@override
ConsumerState<SignUpScreen> createState() => _SignUpScreenState();
}
class _SignUpScreenState extends ConsumerState<SignUpScreen> {
onSubmit() async {
LoggerService.log(LogEventType.ButtonPress,
LogDetails(buttonName: 'onSubmit', displayName: 'Sign Up Screen'));
final registerChangeNotifier =ref.watch(registerChangeNotifierProvider);
//can't call update function
ref.read(registerChangeNotifier.notifier).update()
try {
if (_formKey.currentState!.validate() && isChecked) {
//Close keyboard
Navigator.of(context, rootNavigator: true)
.pushNamed(EmailOtpScreen.routeName);
}
} catch (e, stackTrace) {
LoggerService.log(
LogEventType.Warning,
LogDetails(
error: e,
message: e?.toString() ?? e?.toString(),
stackTrace: stackTrace));
}
}
here is the provider
class RegisterProvider extends Notifier<SignupModel> {
@override
SignupModel build() {
return SignupModel(email: '');
}
void update() {
}
}
From the documentation:
ref.watch
is used inside thebuild
method of a widget or inside the body of a provider to have the widget/provider listen to a provider
If you want to call the update
method from your Notifier
, use the same way as the docs demonstrated how to call the increment
method here.
That is, in your case, the way to call it is:
ref.read(registerChangeNotifierProvider.notifier).update();
and remove the ref.watch
line.
I suggest you to familiarize yourself with the use of ref.watch
, ref.read
, ref.listen
, notifiers and providers from the documentation in general.
pls read docs..