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;
}
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.