problem in adding event in the bloc in flutter_bloc

1.2k Views Asked by At

I am using Flutter_bloc package to make a phone auth in flutte, everything work good, but my question is about adding events to the bloc, for example in my application, when i click on button like this code below, the event added to my loginBloc, and everything works good, but when i press back button in android device, and then return back by using normal navigater.pushNamed, and click the button again nothing happen? that mean the event not added to bloc or something like this? can anybody explain this problem? thanks in advance: this is my sample code to add event when click button:

 child: RaisedButton(
              onPressed: () {
                if (_formKey.currentState.validate()) {
                  loginBloc.add(LoginPressesEvent(
                      phoNo: _phoneTextController.value.text));
                }
              },
1

There are 1 best solutions below

1
On

For adding an 'Event' to 'Bloc' use this code:

BlocProvider.of<'YourBlocClass'>('blocContext').add('YourEvent()'));

'blocContext' is context parameter of `listener in BlocListener' :

BlocProvider(
      create: (context) => BlocClass()..add(Fetch()),
      child: BlocListener<BlocClass, BaseState>(
            listener: (listenerContext, state) {
                // listenerContext: store this parameter to Field
                // and use that everywhere in your StateClass
            },

or context parameter of 'builder in Bloc Builder`

BlocProvider(
      create: (context) => BlocClass()..add(Fetch()),
      child: BlocBuilder<IndexBloc, BaseState>(
            builder: (builderContext, state) {
                // builderContext: store this parameter to Field
                // and use that everywhere in your StateClass
            },