Nikash Deka
Nikash Deka

Reputation: 427

How to convert these block of codes into non-nullable statement/s in dart?

I am trying to convert these lines of code into non-nullable statements, but encountering the error...

The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type. Try adding either a return or a throw statement at the end.

             child: BlocBuilder<LoginBloc, LoginState>(
                builder: (context, state) {
                  if (state is LoginInitialState) {
                    return buildInitialUi();
                  } else if (state is LoginLoadingState) {
                    return buildLoadingUi();
                  } else if (state is LoginFailState) {
                    return buildFailureUi(state.message);
                  } else if (state is LoginSuccessState) {
                    emailCntrlr.text = "";
                    passCntrlr.text = "";
                    return Container();
                  }
                },
              ),

Upvotes: 2

Views: 16050

Answers (2)

YoBo
YoBo

Reputation: 2529

Try this

child: BlocBuilder<LoginBloc, LoginState>(
                builder: (context, state) {
                  if (state is LoginInitialState) {
                    return buildInitialUi();
                  } else if (state is LoginLoadingState) {
                    return buildLoadingUi();
                  } else if (state is LoginFailState) {
                    return buildFailureUi(state.message);
                  } else if (state is LoginSuccessState) {
                    emailCntrlr.text = "";
                    passCntrlr.text = "";
                    return Container();
                  }
                  return Container(); // In case none of the if statements is true
                },
              ),

Upvotes: 4

Aakash Panwar
Aakash Panwar

Reputation: 11

Try This

After String add "?" and return null; Like This

class ValidationMixin {
  String? validateEmail(String value) {
    if (!value.contains('@')) {
      return 'Please enter a Valid Email';
    }
    return null;
  }
}

Upvotes: 1

Related Questions