![](/img/trans.png)
[英]What is the difference between between context.watch and context.read in flutter_bloc?
[英]problem in adding event in the bloc in flutter_bloc
我正在使用 Flutter_bloc 包在 Flutter 中進行電話身份驗證,一切正常,但我的問題是關於向 bloc 添加事件,例如在我的應用程序中,當我單擊下面的代碼時,事件添加到我的 loginBloc ,並且一切正常,但是當我在 android 設備中按下后退按鈕,然后使用普通的導航器.pushNamed 返回,然后再次單擊該按鈕時沒有任何反應? 這意味着事件沒有添加到集團或類似的東西? 有人可以解釋這個問題嗎? 提前致謝:這是我在單擊按鈕時添加事件的示例代碼:
child: RaisedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
loginBloc.add(LoginPressesEvent(
phoNo: _phoneTextController.value.text));
}
},
要將“事件”添加到“Bloc”,請使用以下代碼:
BlocProvider.of<'YourBlocClass'>('blocContext').add('YourEvent()'));
'blocContext' 是'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
},
或“Bloc Builder”中的“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
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.