简体   繁体   中英

problem in adding event in the bloc in flutter_bloc

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));
                }
              },

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
            },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM