【问题标题】:problem in adding event in the bloc in flutter_bloc在flutter_bloc中添加事件的问题
【发布时间】:2021-02-05 07:53:58
【问题描述】:

我正在使用 Flutter_bloc 包在 Flutte 中进行电话身份验证,一切正常,但我的问题是关于向 bloc 添加事件,例如在我的应用程序中,当我点击下面这段代码的按钮时,添加的事件到我的 loginBloc,一切正常,但是当我按下 android 设备中的返回按钮,然后使用普通的 navigationr.pushNamed 返回,然后再次单击该按钮时没有任何反应?这意味着该事件未添加到 bloc 或类似的东西?任何人都可以解释这个问题吗?在此先感谢:这是我在单击按钮时添加事件的示例代码:

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

【问题讨论】:

  • 显示更多代码 ::::
  • 当你点击后退按钮时,它会返回到上一页,但如果你不能正常工作?
  • 您好,您对此有什么解决方案吗?

标签: android flutter flutter-bloc


【解决方案1】:

要向“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
            },

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

【讨论】:

  • 但我不能这样做,首先我在 main.dart 中定义 (BlocProvider(create: (context) => BlocClass()..add(Fetch()),),因为我是使用 MultiBlocProvider) 其次在很多情况下我只需要添加事件而不是构建任何东西,那么为什么我必须使用 BlocBuilder 或 BlocListener?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 2012-09-29
相关资源
最近更新 更多