non_constant_case_expression_from_deferred_library
Constant values from a deferred library can't be used as a case expression.
Description
#The analyzer produces this diagnostic when the expression in a case clause references a constant from a library that is imported using a deferred import. In order for switch statements to be compiled efficiently, the constants referenced in case clauses need to be available at compile time, and constants from deferred libraries aren't available at compile time.
For more information, check out Lazily loading a library.
Example
#Given a file a.dart
that defines the constant zero
:
const zero = 0;
The following code produces this diagnostic because the library a.dart
is
imported using a deferred
import, and the constant a.zero
, declared in
the imported library, is used in a case clause:
import 'a.dart' deferred as a;
void f(int x) {
switch (x) {
case a.zero:
// ...
break;
}
}
Common fixes
#If you need to reference the constant from the imported library, then
remove the deferred
keyword:
import 'a.dart' as a;
void f(int x) {
switch (x) {
case a.zero:
// ...
break;
}
}
If you need to reference the constant from the imported library and also
need the imported library to be deferred, then rewrite the switch statement
as a sequence of if
statements:
import 'a.dart' deferred as a;
void f(int x) {
if (x == a.zero) {
// ...
}
}
If you don't need to reference the constant, then replace the case expression:
void f(int x) {
switch (x) {
case 0:
// ...
break;
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。