Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormGroup onWillPop does not work correctly formgroup is not yet initalized. #435

Open
mavriq-dev opened this issue Jan 7, 2024 · 1 comment

Comments

@mavriq-dev
Copy link

I'm trying to use the ReactiveForms onWillPop to handle saving to a database. I have it set up like this.

return ReactiveForm(
      formGroup: form,
      onWillPop: () => _onWillPop(context, ref),
      child: SomeWidget()

In the following code:

 Future<bool> _onWillPop(BuildContext context, WidgetRef ref) async {
    final formGroup = ReactiveForm.of(context) as FormGroup;

an error is thrown as ReactiveForm.of returns null. Calling the same function with a submit button works fine. Is there any way that onWillPop could be changed so it is thrown after the form is initialized?

@joanpablo
Copy link
Owner

Hi @mavriq-dev Thanks for the feedback.

Based on the example you wrote above, the context object you are passing to your _onWillPop() does not include the ReactiveForm widget. You are providing the context of the parent widget and not the context of the ReactiveForm widget. Instead, you can directly pass the form object to your _onWillPop method.

return ReactiveForm(
      formGroup: form,
      onWillPop: () => _onWillPop(form, ref),
      child: SomeWidget()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants