2

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

Daniil
  • 369
  • 1
  • 11

2 Answers2

1

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
      },
    );
  }
}
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
  • If I remove this line, I get the following error - The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type. (Documentation) Try adding either a return or a throw statement at the end. – Daniil Jan 17 '23 at 04:25
  • Replace `throw StateError('err')` with `return MyError();` You are good to go ! – krishnaacharyaa Jan 17 '23 at 04:26
  • Um, now when I go to this screen, I immediately get into the MyError state. But, I have to get into the RegistrationSendCodeNotLoading state. Why can't I get into it? – Daniil Jan 17 '23 at 04:37
  • I have an example, the previous screen, in which I just get into the Not Loading state. Here is an example https://dpaste.org/dFFiJ. And it works the way I need it, I get into the Not Loading state – Daniil Jan 17 '23 at 04:38
  • You are not returning , try adding `return` statement infront of `RegistrationSendCodeWidget();`, See my edited post – krishnaacharyaa Jan 17 '23 at 04:41
0

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();
        }
      },
    );
  }
}
Anas
  • 1,013
  • 1
  • 6
  • 20