Use Cubit for Flutter form


I’m trying to use Cubit state management for a form (validation and submit). Is there any example I can follow?

I have already tried to implement it and it works, but every time there is an error, it shows me the form again with empty fields because the widget is repainted with initial data. How can I solve it?

Here my Cubit consumer code on the form:

BlocConsumer<LoginCubit, LoginState> buildLoginCard(BuildContext context) {
  return BlocConsumer<LoginCubit, LoginState>(
    listener: (context, state) {
      if (state is LoginError) {
            content: Text(state.message),
      } else if (state is LoginSuccess) {
    builder: (context, state) {
      if (state is LoginInProgress) {
        return LoadingWidget();
      } else {
        return LoginCardWidget();


you might try to add this to your BlocConsumer after listener

buildWhen: (previous, current) {
    if (current is LoginError)
      return false;
      return true;

in this case if the state is error it will not rebuild the widget agin and the fields will have there own values without being changed

Answered By – Baraa Aljabban

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *