【问题标题】:Previous screen Bloc call after open new screen in Flutter在 Flutter 中打开新屏幕后的上一屏 Bloc 调用
【发布时间】:2020-12-22 11:32:12
【问题描述】:

我在我的项目中使用 Bloc 模式,但是我以前的屏幕的 Bloc 仍然在我的新屏幕中调用。我已经处置了我的 Bloc,但在屏幕中处置方法没有被调用。请帮忙解决这个问题。

块文件:

class ForgotPasswordBloc extends Object {
  final PublishSubject<ForgotPassState> _passStateSubject = new PublishSubject();

  Stream<ForgotPassState> get passStateStream => _passStateSubject.stream;

  void changeState({ForgotPassState state}) => _passStateSubject.sink.add(state);

  forgotPasswordSubmit(String email) async {
    changeState(state: ForgotPassState(status: ForgotPassStatus.CALLING, message: "calling"));
    
      var response = await post(Constant.API_URL+"forgotPassword", body: {
        "email": email,
      });
      ForgotPasswordModal data = ForgotPasswordModal.fromJson(json.decode(response.body));
      if (data.status == "success") {

        await changeState(state: ForgotPassState(status: ForgotPassStatus.SUCCESS, message: "Success"));
      } else {
        changeState(
            state: ForgotPassState(
                status: ForgotPassStatus.ERROR, message: data.message));
      }
  }

  dispose() {
    print("dispose called==========>");
    _passStateSubject.close();
  }
}
Here I call the bloc method:
 StreamBuilder<ForgotPassState>(
            stream: forgotpass_bloc.passStateStream,
            builder: (context, AsyncSnapshot<ForgotPassState> snapshot) {
              switch (snapshot.data.status) {
                case ForgotPassStatus.SUCCESS:
                  WidgetsBinding.instance.addPostFrameCallback((_) => showInformationDialog(context,email_controller.text.trim()));
                  break;
                default:
                  return Container();
              }
            },
          ),
In above code after getting success response my information dialog is open, In dialog I have close button After click on the close button I have open new Screen but in that screen my previous screen dialog is display.

【问题讨论】:

  • 贴一段你的代码

标签: flutter flutter-bloc


【解决方案1】:

我猜你需要在 ui 小部件中处理 bloc,在那里你生成 bloc 实例而不是 bloc 类本身

【讨论】:

  • 我也在我的 ui 小部件中处理 bloc,但这不起作用。但我通过在成功状态后添加新状态来解决。
猜你喜欢
  • 2022-07-09
  • 2019-12-27
  • 2022-01-20
  • 1970-01-01
  • 2021-06-12
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多