【问题标题】:Is it possible to call two methods in a bloclistener?是否可以在 bloclistener 中调用两种方法?
【发布时间】:2020-11-14 07:25:27
【问题描述】:

我正在使用 bloc 模式上传图片。我有一个监听状态 UploadSuccess 的 bloc 监听器。我想在监听器中调用两个方法,一个用来弹出路由,另一个用来显示一个snackbar:

if (state is UpLoadSuccess) {
                      RioHelpers.showSuccessFlushBar(context, "Document Uploaded!");
                      Navigator.pop(context);
                    }

调用其中一种方法可以正常工作。我可以弹出路线或显示小吃店,但不能同时显示。当这两种方法都存在时,什么都不会发生。如何调用这两种方法?

这是冲洗条:

static void showSuccessFlushBar(BuildContext context, String title) {
    Flushbar(
      duration: Duration(seconds: 3),
      icon: Icon(
        Icons.check,
        color: Colors.white,
      ),
      backgroundColor: RioColours.snackBarSuccess,
      message: title,
      flushbarStyle: FlushbarStyle.FLOATING,
      margin: EdgeInsets.all(8),
      borderRadius: 8,
    )..show(context);
  }

【问题讨论】:

    标签: flutter flutter-bloc


    【解决方案1】:

    如果 RioHelpers.showSuccessFlushBar 是一个未来,而 Snackbar 在片刻之后消失,那么你可以尝试等待它

    if (state is UpLoadSuccess) {
                      RioHelpers.showSuccessFlushBar(context, "Document Uploaded!");
                      await Future.delayed(Duration(seconds: 1));
                      Navigator.pop(context);
                    }
    

    【讨论】:

    • 这不是未来。它有一个持续时间属性。
    • 似乎正在发生的是 Navigator.pop(context) 正在立即弹出 Snackbar。因此,似乎什么都没有发生。您可以尝试添加 await Future.delayed(Duration(seconds: NNN));其中 NNN 是等待的秒数。您还需要将异步添加到函数中。
    猜你喜欢
    • 2014-12-13
    • 2010-10-25
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多