【问题标题】:Flutter: Bloc dispose without waiting for the server responseFlutter:无需等待服务器响应即可处理 Bloc
【发布时间】:2019-05-28 14:57:12
【问题描述】:

用户一进入页面,就会执行对服务器的调用。如果快照有数据,则会发送一个流到创建 ListView 的 UI,否则,如果快照有错误,则会接收一个流错误消息。

所以,我的电话是:

try {
      List answer = await call();
      createList.sink.add(answer);
    } on Exception catch (e) {
      createList.sink.addError(e);
    }

问题是:如果连接很慢,并且用户在调用完成之前退出了该页面,控制器将被释放,并且应用程序会在我释放控制器后抱怨错误无法下沉。 那么,有没有办法在用户退出页面时“中止”对服务器的调用?

【问题讨论】:

    标签: server dart flutter call bloc


    【解决方案1】:

    使用控制器的isClosed 属性,您可以在添加事件之前检查控制器是否已关闭,如下所示:

    try {
      List answer = await call();
      if (!createList.isClosed) {
        createList.sink.add(answer);
      }
    } on Exception catch(e) {
      if (!createList.isClosed) {
        createList.sink.addError(e);
      }
    }
    

    【讨论】:

    • 我认为你多放了一个“水槽”。我只是做“!createList.isClosed”
    猜你喜欢
    • 1970-01-01
    • 2023-01-17
    • 2020-04-18
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多