Bloc: getter 'isFormValid' was called on null on firebase login using flutter_bloc

Created on 23 Sep 2020  ยท  3Comments  ยท  Source: felangel/bloc

I need to login to home page using firebase login with bloc pattern. LoginBloc is giving null inside the BlocBuilder.

Expected behavior
I expected to open the loginForm.but got an error getter 'isFormValid' 'isEmailValid' and 'isPasswordValid' was called on null

class _LoginFormState extends State<LoginForm> {
  final _formKey = GlobalKey<FormState>();
  bool _passwordVisible = false;
  bool _autoValidate = false;
  final TextEditingController _emailController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  bool get isPopulated =>
      _emailController.text.isNotEmpty && _passwordController.text.isNotEmpty;

  bool isLoginButtonEnabled(LoginState state) {
    return state.isFormValid && isPopulated && !state.isSubmitting;
  }

the error caused by login button

RaisedButton(
         disabledColor: Colors.blueGrey,
         shape: StadiumBorder(),
          padding: EdgeInsets.all(0),
          onPressed: isLoginButtonEnabled(state)
                                     ? _onFormSubmitted
                                      : null)

which is on

 child: BlocBuilder<LoginBloc, LoginState>(
          builder: (BuildContext context, LoginState state) {
            return Stack(
              children: [
                ////
               RaisedButton()
             )})

screenshot

โ•โ•โ•โ•โ•โ•โ•โ• Exception caught by widgets library โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
The following NoSuchMethodError was thrown building BlocBuilder(dirty, state: _BlocBuilderBaseState#34e93):
The getter 'isFormValid' was called on null.
Receiver: null
Tried calling: isFormValid

The relevant error-causing widget was:
BlocBuilder file:///C:/Users/Ezra/AndroidStudioProjects/bekloh_user/lib/component/login_form.dart:76:14
When the exception was thrown, this was the stack:

0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)

1 _LoginFormState.isLoginButtonEnabled (package:bekloh_user/component/login_form.dart:27:18)

2 _LoginFormState.build. (package:bekloh_user/component/login_form.dart:212:46)

3 BlocBuilder.build (package:flutter_bloc/src/bloc_builder.dart:90:57)

4 _BlocBuilderBaseState.build (package:flutter_bloc/src/bloc_builder.dart:150:21)

question

Most helpful comment

yah it works thank u very much

All 3 comments

Hi @Getahun20 ๐Ÿ‘‹
Thanks for opening an issue!

Looks like the issue is the bloc's initial state is being set to null at https://github.com/ezra126/bekloh_user/blob/c819e773038c71026e62b813a26ee41513ff8970/lib/bloc/loginbloc/login_bloc.dart#L20.

If you update your bloc like so it should resolve the error:

LoginBloc({
    @required UserRepository userRepository,
  })  : assert(userRepository != null),
        _userRepository = userRepository, super(LoginState.empty()); // pass the initial state to super instead

  // REMOVE THE FOLLOWING LINE
  LoginState get initialState => LoginState.empty();

Hope that helps! ๐Ÿ‘

yah it works thank u very much

Was this page helpful?
0 / 5 - 0 ratings