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