title: How to correctly use BlocListener and BlocProvider in Flutter App
body: <p>I am using flutter_bloc 4.0.0 in my Flutter App, I used the example from Felix Angelov (<a href="https://medium.com/flutter-community/firebase-login-with-flutter-bloc-47455e6047b0" rel="noreferrer">https://medium.com/flutter-community/firebase-login-with-flutter-bloc-47455e6047b0</a>) on implementing a sign in or login flow using the bloc pattern. It was working fine but after I updated my Flutter and checked on my code later I have found a series of errors. I don't understand why they are coming because last week everything was just fine. The implementation of the bloc in the build method for a widget has become wrong for me all of a sudden. I am getting the error:</p>
  
  <p>1."‘A value type of BlocListener can’t be returned from the method build because it has a return type of widget"</p>
  
  <ol start="2">
  <li>‘A value type of BlocProvider> can’t be returned from the method build because it has a return type of widget’</li>
  </ol>
  
  <p>the code for the first error </p>
  
  <pre><code>class LoginForm extends StatefulWidget {
    final UserRepository _userRepository;
  
    LoginForm({Key key, @required UserRepository userRepository})
        : assert(userRepository != null),
          _userRepository = userRepository,
          super(key: key);
  
    State&lt;LoginForm&gt; createState() =&gt; _LoginFormState();
  }
  
  class _LoginFormState extends State&lt;LoginForm&gt; {
    final TextEditingController _emailController = TextEditingController();
    final TextEditingController _passwordController = TextEditingController();
  
    LoginBloc _loginBloc;
  
    UserRepository get _userRepository =&gt; widget._userRepository;
  
    bool get isPopulated =&gt;
        _emailController.text.isNotEmpty &amp;&amp; _passwordController.text.isNotEmpty;
  
    bool isLoginButtonEnabled(LoginState state) {
      return state.isFormValid &amp;&amp; isPopulated &amp;&amp; !state.isSubmitting;
    }
  
    @override
    void initState() {
      super.initState();
      _loginBloc = BlocProvider.of&lt;LoginBloc&gt;(context);
      _emailController.addListener(_onEmailChanged);
      _passwordController.addListener(_onPasswordChanged);
    }
  
    @override
    Widget build(BuildContext context) {
      return BlocListener&lt;LoginBloc, LoginState&gt;(
        listener: (context, state) {
          if (state.isFailure) {
            Scaffold.of(context)
              ..hideCurrentSnackBar()
              ..showSnackBar(
                SnackBar(
                  content: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [Text('Login Failure'), Icon(Icons.error)],
                  ),
                  backgroundColor: Colors.red,
                ),
              );
          }
          if (state.isSubmitting) {
            Scaffold.of(context)
              ..hideCurrentSnackBar()
              ..showSnackBar(
                SnackBar(
                  content: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('Logging In...'),
                      CircularProgressIndicator(),
                    ],
                  ),
                ),
              );
          }
          if (state.isSuccess) {
            BlocProvider.of&lt;AuthenticationBloc&gt;(context).add(LoggedIn());
          }
        },
        child: BlocBuilder&lt;LoginBloc, LoginState&gt;(
          builder: (context, state) {
            return Padding(
              padding: EdgeInsets.all(20.0),
              child: Form(
                child: ListView(
                  children: &lt;Widget&gt;[
                    Padding(
                      padding: EdgeInsets.symmetric(vertical: 20),
                      child: Image.asset('assets/flutter_logo.png', height: 200),
                    ),
                    Container(
                      margin: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 20.0),
                      height: 45.0,
                      child: TextFormField(
                        controller: _emailController,
                        style: TextStyle(
                          fontFamily: 'Avenir-Medium',
                          fontSize: 12.0,
                          color: Colors.black,
                        ),
                        decoration: InputDecoration(
                          border: OutlineInputBorder(
                              borderRadius: const BorderRadius.all(
                                const Radius.circular(7.0),
                              ),
                              borderSide: BorderSide(
                                color: Colors.grey[200],
                                width: 7.0,
                              )),
                          labelText: 'email',
                        ),
                        keyboardType: TextInputType.emailAddress,
                        autovalidate: true,
                        autocorrect: false,
                        validator: (_) {
                          return !state.isEmailValid ? 'Invalid Email' : null;
                        },
                      ),
                    ),
                    Container(
                      height: 45.0,
                      child: TextFormField(
                        style: TextStyle(
                          fontFamily: 'Avenir-Medium',
                          fontSize: 12.0,
                          color: Colors.black,
                        ),
                        controller: _passwordController,
                        decoration: InputDecoration(
                          border: OutlineInputBorder(
                              borderRadius: const BorderRadius.all(
                                const Radius.circular(7.0),
                              ),
                              borderSide: BorderSide(
                                color: Colors.grey[200],
                                width: 0.0,
                              )),
                          labelText: 'password',
                        ),
                        obscureText: true,
                        autovalidate: true,
                        autocorrect: false,
                        validator: (_) {
                          return !state.isPasswordValid ? 'Invalid Password' : null;
                        },
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.symmetric(vertical: 20),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: &lt;Widget&gt;[
                          LoginButton(
                            onPressed: _onFormSubmitted,
  
  //                          isLoginButtonEnabled(state)
  //                              ? _onFormSubmitted
  //                              : null,
                          ),
                          GoogleLoginButton(),
                          AppleSignInButton(),
                          CreateAccountButton(userRepository: _userRepository),
                          ForgotPasswordButton()
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      );
    }
  
    @override
    void dispose() {
      _emailController.dispose();
      _passwordController.dispose();
      super.dispose();
    }
  
    void _onEmailChanged() {
      _loginBloc.add(
        EmailChanged(email: _emailController.text),
      );
    }
  
    void _onPasswordChanged() {
      _loginBloc.add(
        PasswordChanged(password: _passwordController.text),
      );
    }
  
    void _onFormSubmitted() {
      _loginBloc.add(
        LoginWithCredentialsPressed(
          email: _emailController.text,
          password: _passwordController.text,
        ),
      );
    }
  }
  
  the code for the second error above is as follows
  
  void main() {
    WidgetsFlutterBinding.ensureInitialized();
    BlocSupervisor.delegate = SimpleBlocDelegate();
    final UserRepository userRepository = UserRepository();
    runApp(
      BlocProvider(
        create: (context) =&gt; AuthenticationBloc(
          userRepository: userRepository,
        )..add(AppStarted()),
        child: App(userRepository: userRepository),
      ),
    );
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/uDGzn.png" rel="noreferrer"><img src="https://i.stack.imgur.com/uDGzn.png" alt="image for the error"></a></p>
  
  <p><a href="https://i.stack.imgur.com/0l9xB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/0l9xB.png" alt="the second error"></a></p>