nullable_type_in_catch_clause
A potentially nullable type can't be used in an 'on' clause because it isn't valid to throw a nullable expression.
Description
#The analyzer produces this diagnostic when the type following on
in a
catch
clause is a nullable type. It isn't valid to specify a nullable
type because it isn't possible to catch null
(because it's a runtime
error to throw null
).
Example
#The following code produces this diagnostic because the exception type is
specified to allow null
when null
can't be thrown:
dart
void f() {
try {
// ...
} on FormatException? {
}
}
Common fixes
#Remove the question mark from the type:
dart
void f() {
try {
// ...
} on FormatException {
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。