open or navigate on new Bloc form on pressed event

Viewed 51

I have 2 Bloc Registration and Login, Registration click button on the login page. I want to redirect on Registration page from login's Registration Button click, here is the snippate. getting an error like this

child: new RaisedButton(
          color: Colors.red[800],
          textColor: Colors.white,
          child: new Text(
            'REGISTER',
            style: TextStyle(fontSize: 18, fontWeight: FontWeight.w400),
          ),
          onPressed: () => BlocListener(
              //bloc: RegistrationPageBloc,
              listener:
                  (BuildContext context, RegistrationFormState state) {
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (BuildContext context) {
                  return RegistrationForm();
                }));
              }),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20.0),
          ),
        ),

enter image description here

3 Answers

Remove bloclistener

child: new RaisedButton(
          color: Colors.red[800],
          textColor: Colors.white,
          child: new Text(
            'REGISTER',
            style: TextStyle(fontSize: 18, fontWeight: FontWeight.w400),
          ),
          onPressed: () => 
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (BuildContext context) {
                  return RegistrationForm();
                })),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20.0),
          ),
        ),

Your parent widget doesn't pass your desirect registrationBloc to the child with the BlocListener, which results in a context without a provided RegistrationBloc.

You need to wrap your parent with a BlocProvider. For example:

BlocProvider(
 create: (_) => RegistrationBloc(),
 child: RaisedButton(
    child: BlocListener(),
  )
),

You can use two bloc to provide same page with MultiBlocProvider

MultiBlocProvider(
        providers: [
           BlocProvider(
                create: (context) => RegistrationBloc(),
           ),
            BlocProvider(
              create: (context) => LoginBloc(),
           ),
           ],
            child: YourPage(),
       );
Related