skip to Main Content

I’m trying to get to the RegistrationSendCode screen. But unfortunately I am getting a bad status error. Here is my provider and builder –
Provider –

class RegistrationSendCode extends StatelessWidget{
  @override
  Widget build(BuildContext context){
    return BlocProvider<RegistrationSendCodeCubit>(
      create: (context) => RegistrationSendCodeCubit(),
      child: RegistrationSendCodeBuilder(),
    );
  }
}

Builder –

class RegistrationSendCodeBuilder extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<RegistrationSendCodeCubit, RegistrationSendCodeState>(
      builder: (context, state) {
        if(state is RegistrationSendCodeNotLoading) {
          RegistrationSendCodeWidget();
        } else if(state is RegistrationSendCodeLoading) {
          return const Scaffold(
            body: Center(child: CircularProgressIndicator(),),
          );
        } else if(state is RegistrationSendCodeLoaded) {
          return FullName();
        } else if(state is RegistrationSendCodeError) {
          return MyError();
        }
        throw StateError('err');
      },
    );
  }
}

error –

The following StateError was thrown building BlocBuilder<RegistrationSendCodeCubit, RegistrationSendCodeState>(dirty, dependencies: [_InheritedProviderScope<RegistrationSendCodeCubit?>], state: _BlocBuilderBaseState<RegistrationSendCodeCubit, RegistrationSendCodeState>#cd448):
Bad state: err

The relevant error-causing widget was: 
  BlocBuilder<RegistrationSendCodeCubit, RegistrationSendCodeState> BlocBuilder: return BlocProvider<RegistrationSendCodeCubit>(

It is expected that when I go to this screen, I should immediately get into the RegistrationSendCodeNotLoading state, which is logical

2

Answers


  1. Builder parameter must return a Widget to display for all states. In RegistrationSendCodeNotLoading state, you’re not returning the widget.

    It common approach to have else statement instead of else if for RegistrationSendCodeError state without exclusively checking it with condition.

    Change code as below,

    class RegistrationSendCodeBuilder extends StatelessWidget {
    
      @override
      Widget build(BuildContext context) {
        return BlocBuilder<RegistrationSendCodeCubit, RegistrationSendCodeState>(
          builder: (context, state) {
            if(state is RegistrationSendCodeNotLoading) {
              return RegistrationSendCodeWidget();
            } else if(state is RegistrationSendCodeLoading) {
              return const Scaffold(
                body: Center(child: CircularProgressIndicator(),),
              );
            } else if(state is RegistrationSendCodeLoaded) {
              return FullName();
            } else {
              return MyError();
            }
          },
        );
      }
    }
    
    Login or Signup to reply.
  2. You are throwing the error, But you are not catching it anywhere

    Try removing the line.

    class RegistrationSendCodeBuilder extends StatelessWidget {
    
      @override
      Widget build(BuildContext context) {
        return BlocBuilder<RegistrationSendCodeCubit, RegistrationSendCodeState>(
          builder: (context, state) {
            if(state is RegistrationSendCodeNotLoading) {
               return RegistrationSendCodeWidget();  👈 add return 
            } else if(state is RegistrationSendCodeLoading) {
              return const Scaffold(
                body: Center(child: CircularProgressIndicator(),),
              );
            } else if(state is RegistrationSendCodeLoaded) {
              return FullName();
            } else if(state is RegistrationSendCodeError) {
              return MyError();
            }
            throw StateError('err');  // ❌ remove this line
          },
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search