飞镖捕捉异常未来功能

时间:2021-07-21 17:29:52

标签: dart exception try-catch

我想捕获从 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>'

这有什么问题?

0 个答案:

没有答案