Bloc: How do I clean the form using flutter_bloc?

Created on 22 Sep 2020  路  4Comments  路  Source: felangel/bloc

I've been using validation which I think is very good the problem is when I want to clean up the textformfields, since I have to update the status of the inputs How could I clean up the inputs? already try to return

` else if (event is FormSubmitted) {
      if (state.status.isValidated) {
        yield state.copyWith(status: FormzStatus.submissionInProgress);
        await Future<void>.delayed(const Duration(seconds: 1));
        yield state.copyWith(status: FormzStatus.submissionSuccess);
        yield MyFormState();  <------- but dont work
      }
    }`
question

Most helpful comment

Thank you so much !!

All 4 comments

I'm having this issue too, see this SO question

I also note that the form validation example in this repo doesn't clear the form after submitting, so there's not much guidance on what seems to be a simple task but is actually quite an architectural question.

If it's a little complex to fix this and then I'd better use TextEditingController

Hi @Makarov96 馃憢
Thanks for opening an issue!

The reason it doesn't work is because you need to use a BlocListener to update the TextEditingController when the state changes. Otherwise the UI will update the bloc but the bloc won't update the UI (because the TextField maintains its own state by default).

BlocListener<FormBloc, FormState>(
  listener: (context, state) {
    // Handle updating the `TextEditingController` text based on the bloc state
    _controller.text = state.value;
  },
  child: ...
)

Hope that helps! 馃憤

Thank you so much !!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zjjt picture zjjt  路  3Comments

wheel1992 picture wheel1992  路  3Comments

rsnider19 picture rsnider19  路  3Comments

craiglabenz picture craiglabenz  路  3Comments

clicksocial picture clicksocial  路  3Comments