Unimplemented handling of missing static target

Issue

I have a login button on the start screen. I want when the user clicks this login button, I will go to the login screen using "MaterialPageRoute" and I want to pass "BlocProvider" for Login screen. I did the following and an error occurred.

"Error messages

════════ Exception caught by widgets library ═══════════════════════════════════
The following _CompileTimeError was thrown building MyApp(dirty):
Unimplemented handling of missing static target.
"

I’m a forgetful novice with Flutter. I look forward to everyone’s help. Thank you very much.

   press: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) {
                  return BlocProvider<LoginBloc>(
                      create: (context) =>
                          LoginBloc(userRepository: _userRepository),
                      child: BlocBuilder<LoginBloc, LoginState>(
                        builder: (context, loginState) {
                          print('$loginState');
                          return LoginScreen();
                        },
                      ) //LoginPage,
                      );
                },
              ),
            );
          },

Solution

To pass bloc to another screen use BlocProvider.Value()

see below code:

press: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) {
                  return BlocProvider.value(
                         value: BlocProvider.of<LoginBloc>(context),
                         child: LoginScreen(),
                   );
                },
              ),
            );
          },

Then from LoginScreen we can retrieve LoginBloc with:

// with extensions
context.read<LoginBloc>();

// without extensions
BlocProvider.of<LoginBloc>(context);

The above snippets result in a one-time lookup and the widget will not be notified of changes. To retrieve the instance and subscribe to subsequent state changes use:

// with extensions
context.watch<LoginBloc>();

// without extensions
BlocProvider.of<LoginBloc>(context, listen: true);

Answered By – Aakash kondhalkar

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *