1

I have an enum in client code describing a list of endpoints in the API:

enum Requests { GetUsers, GetProducts, ... }

I want to send requests to server having these values, like connection.sendRequest(Requests.GetUsers).

Now, in the sendRequest() function I want the enum value to be converted to something like '/users'.

Can I attach methods to each enum similar to this below?

enum Requests {
    GetUsers.toString: '/users',
    GetPendingDomains: '/prodcuts'
}
Sergei Basharov
  • 47,526
  • 63
  • 186
  • 320

1 Answers1

2

Not directly in the enum (enums are really basic in Dart). You have to create a Map<Requests, String> aside to handle the associated paths.

enum Request { GetUsers, GetProducts, ... }
final paths = <Request, String>{
  Request.GetUsers:    '/users',
  Request.GetProducts: '/products',
}
Alexandre Ardhuin
  • 62,400
  • 12
  • 142
  • 126