How to switch on Enum in Dart?
Asked Answered
S

3

19

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?

Scabbard answered 2/7, 2019 at 14:25 Comment(0)
H
19

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.

Hwang answered 2/7, 2019 at 14:45 Comment(0)
C
2

Switching on enums is easy, for example:

enum ActivityType {
  running,
  climbing,
  hiking,
  cycling,
  ski
}

extension ActivityTypeNumber on ActivityType {
  int get number {
    switch (this) {
      case ActivityType.running:
        return 1;
      case ActivityType.climbing:
        return 2;
      case ActivityType.hiking:
        return 5;
      case ActivityType.cycling:
        return 7;
      case ActivityType.ski:
        return 10;
    }
  }
}

More on enums and how to use their enhanced version

Christianize answered 5/11, 2022 at 16:20 Comment(0)
P
0

Playing with enum and switch case is easy.

enum PaymentStatus { accepted, declined, unauthorized }

This is the enum on which below switch is working

switch (paymentStatus) {
      case PaymentStatus.accepted:
        postPaymentHandler.handleSuccesFlow();
      case PaymentStatus.declined:
        postPaymentHandler.handleDeclinedError();
      case PaymentStatus.unauthorized:
        postPaymentHandler.handleUnauthorizedError();
      default:
         globalErrorHandler.handleError()
    }
Proparoxytone answered 9/4 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.