throw_of_invalid_type
The type '{0}' of the thrown expression must be assignable to 'Object'.
Description
#The analyzer produces this diagnostic when the type of the expression in a
throw expression isn't assignable to Object
. It isn't valid to throw
null
, so it isn't valid to use an expression that might evaluate to
null
.
Example
#The following code produces this diagnostic because s
might be null
:
dart
void f(String? s) {
throw s;
}
Common fixes
#Add an explicit null-check to the expression:
dart
void f(String? s) {
throw s!;
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。