小编Ked*_*rki的帖子

“onError”处理程序无法返回“Null”类型的值

我无法从 dart Future 的 catchError 处理程序返回 null。我可以使用 try catch 来做到这一点,但我需要使用 then catchError。

使用尝试捕获

 Future<bool?> test() async {
    try {
      return await someFuture();
    } catch (e) {
      return null;
    }
  }

// Works without error
Run Code Online (Sandbox Code Playgroud)

但是当使用then时catchError

  Future<bool?> test() {
    return someFuture().catchError((e) {
      return null;
    });
  }

// Error: A value of type 'Null' can't be returned by the 'onError' handler because it must be assignable to 'FutureOr<bool>'
Run Code Online (Sandbox Code Playgroud)

如果使用 then 和 catchError 遇到错误,如何返回 null?

asynchronous dart flutter

7
推荐指数
1
解决办法
6700
查看次数

使用 flutter lint 后,TODO 注释不会出现在问题中作为 VS Code 的警告

以前,所有待办事项注释都会在 VS Code 中显示为警告,但在我添加 flutter lint 包进行额外的 linting 后,待办事项从问题中消失。现在我必须使用搜索框来查找待办事项。除了“flutter_style_todos”之外,我找不到相应的 linter 规则,这不是我想要的。有什么办法让它再次出现在问题窗格中吗?

dart flutter

5
推荐指数
1
解决办法
2054
查看次数

标签 统计

dart ×2

flutter ×2

asynchronous ×1