0

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));
                }
              },
Osama Mohammed
  • 2,433
  • 13
  • 29
  • 61

1 Answers1

0

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
            },
Saeed Fekri
  • 1,067
  • 9
  • 12
  • 1
    but i cannot to that, first of all i define (BlocProvider( create: (context) => BlocClass()..add(Fetch()),) in main.dart, because i am using MultiBlocProvider) secondly in many cases i just need to add event not to build any thing, so why i have to use BlocBuilder or BlocListener? – Osama Mohammed Oct 23 '20 at 08:45