label_in_outer_scope
Can't reference label '{0}' declared in an outer method.
Description
#The analyzer produces this diagnostic when a break
or continue
statement references a label that is declared in a method or function
containing the function in which the break
or continue
statement
appears. The break
and continue
statements can't be used to transfer
control outside the function that contains them.
Example
#The following code produces this diagnostic because the label loop
is
declared outside the local function g
:
void f() {
loop:
while (true) {
void g() {
break loop;
}
g();
}
}
Common fixes
#Try rewriting the code so that it isn't necessary to transfer control outside the local function, possibly by inlining the local function:
void f() {
loop:
while (true) {
break loop;
}
}
If that isn't possible, then try rewriting the local function so that a value returned by the function can be used to determine whether control is transferred:
void f() {
loop:
while (true) {
bool g() {
return true;
}
if (g()) {
break loop;
}
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。