我想捕获从 Future 函数抛出的异常。 这是代码:
Future<String> get() async {
try {
String? a;
return await Future.delayed(const Duration(seconds: 1), () => "1")
.then((val) => a!);
}
on NoSuchMethodError catch(e) {
throw Exception("custom message $e");
}
}
void main() {
get().catchError((message) { print("catched $message"); });
}
我可以打印异常消息,但出现错误:
Uncaught Error: TypeError: null: type 'JSNull' is not a subtype of type 'FutureOr<String>'
这有什么问题?