• Home
  • Jobs
  • Courses
  • Teachers
  • For business
  • Blog
  • ES/EN

0

36
Views
The value of the local variable '***' isn't used. Dart SWITCH statement

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?

about 1 month ago ·

Santiago Trujillo

1 answers
Answer question

0

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.

about 1 month ago · Santiago Trujillo Report
Answer question
Find remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.