unnecessary_ cast
Unnecessary cast.
Description
#The analyzer produces this diagnostic when the value being cast is already known to be of the type that it's being cast to.
Example
#
The following code produces this diagnostic because
n
is already known to
be an
int
as a result of the
is
test:
void f(num n) {
if (n is int) {
(n as int).isEven;
}
}
Common fixes
#Remove the unnecessary cast:
void f(num n) {
if (n is int) {
n.isEven;
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.