I'm new to Dart and I'm hoping someone can explain this variable scoping warning.
When I use the following:
enum fruits {tomatoes, apples, oranges}
void main() {
for (var item in fruits.values) {
var color = 'red'; // Local variable 'color' declared
switch (item){
case fruits.apples:
break;
case fruits.tomatoes:
break;
default:
color = 'orange'; // Local variable 'color' used, analyzer doesn't see
break;}
print (color); // Local variable 'color' used, analyzer sees
}
}
The code analyzer has no problem with the above. But if I comment out the print statement // print (color);
, I get the warning The value of the local variable 'color' isn't used.
from the analyzer even though color
is used in the default case of the switch
statement.
Why doesn't the analyzer "see" the local variable in the default
case?
Santiago Trujillo
because assigning the variable colors is not consider using the variable.... for example printing the color consider using the variable. When you use the color for a widget or any thing else then the lint will gone.