Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch on Enum in Dart?

I'm watching The Boring Flutter Development Show where in one of the episodes they're showing the implementation of Bloc.

Now there's this chunk of code that I thought would be better replace with Switch statement, you know, in case there appears more cases in the future:

_storiesTypeController.stream.listen((storiesType) {
       if (storiesType == StoriesType.newStories) {
         _getArticlesAndUpdate(_newIds);
       } else {
         _getArticlesAndUpdate(_topIds);
       }
     });

... so I tried to implement it but it gave me some error saying that

Type 'Type' of the switch expression isn't assignable to the type 'Stories Type' of case expressions.

So I came up with this workaround:

final storyType = StoriesType.newStories;

_storiesTypeController.stream.listen((storyType) {
    switch (storyType) {
      case StoriesType.newStories: {
        _getArticlesAndUpdate(_newIds);
      }
        break;
      case StoriesType.topStories: {
        _getArticlesAndUpdate(_topIds);
      }
        break;
      default: {
        print('default');
      }
    }
  });

... and everything works fine but I wonder if there's another way to switch Enum and why it says the value of local variable storyType isn't used, when I use it in this line:

_storiesTypeController.stream.listen((storyType)

and I switch over it?

like image 262
i6x86 Avatar asked Jul 02 '19 14:07

i6x86


People also ask

Can you use switch with enum?

An Enum keyword can be used with if statement, switch statement, iteration, etc. enum constants are public, static, and final by default. enum constants are accessed using dot syntax. An enum class can have attributes and methods, in addition to constants.

How do you use enums in flutter?

Getting Values From a Flutter Enum All you need to do to get the value of an enum in Dart is refer to the specific property or index of the value you want. Run the code and confirm if it's printing the colors and their index according to the expected result.

How do you assign a value to an enum in darts?

Even if it's not possible to set the value to enum we can do it in class. The data type is obvious and we don't need additional properties like index or toString() when comparing the value. One point that we should beware of is that we can instantiate the class. var instance = MyEnum();


1 Answers

You have a redundant variable that lives in the outer scope:

final storyType = StoriesType.newStories;

Since the callback for _storiesTypeController.stream.listen defines a new variable named storyType, the variable from the outer scope is not used.
You can simply drop the redundant line:

final storyType = StoriesType.newStories;

After you have removed it, there should not be any warnings.
Additionally, you do not need curly braces in a switch-statement. The adjusted code would look like this:

_storiesTypeController.stream.listen((storyType) {
    switch (storyType) {
      case StoriesType.newStories:
        _getArticlesAndUpdate(_newIds);
        break;
      case StoriesType.topStories:
        _getArticlesAndUpdate(_topIds);
        break;
      default:
        print('default');
    }
  });

You can find out more about switch and case in Dart's language tour.

like image 181
creativecreatorormaybenot Avatar answered Oct 26 '22 19:10

creativecreatorormaybenot