switch_on_type
Avoid switch statements on a 'Type'.
Description
#The analyzer produces this diagnostic when a switch statement or switch expression is used on either the value of a Type
or a toString
call on a Type
.
Example
#The following code produces this diagnostic because the switch statement is used on a Type
:
dart
void f(Object o) {
switch (o.runtimeType) {
case const (int):
print('int');
case const (String):
print('String');
}
}
Common fixes
#Use pattern matching on the variable instead:
dart
void f(Object o) {
switch (o) {
case int():
print('int');
case String():
print('String');
}
}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-20. View source or report an issue.