This repository has been archived by the owner on Mar 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 278
@Bind.query('--') to Enum gives "No static parse method error" while static parse method is defined in Extension on that Enum. #861
Comments
Please when you post code use a "code block". |
enum Roles {
owner,
admin,
user,
}
extension RolesParser on Roles {
// You can not add static methods extension. The code below allows you do this `RolesParser.parse()` not `Roles.parse()`
static Roles parse(String value) {
final Roles role = tryParse(value);
if (role != null) return role;
}
static Roles tryParse(String input) {
final String value = input.trim();
return Roles.values.firstWhere((element) =>
element.toString().split('.')[1].toUpperCase() == value.toUpperCase());
}
} |
What I would do is either bind to a string and then parse it or do something like: class RoleBind {
RoleBind._(this.value);
final Role value;
static RoleBind parse(String input) {
final result = RoleBind.tryParse(input);
if (result != null) {
return result;
}
throw 'Cound not parse Role';
}
static RoleBind tryParse(String input) {
final value = input.trim();
final eValue = Role.values.firstWhere((role) =>
role.toString().split('.').last.toUppperCase() == value.toUpperCase());
if(eValue != null) {
return RoleBind._(eValue);
}
return null;
}
} |
I would also recommend renaming |
|
@Reductions , your advice to make another class called RoleBind worked like a charm. Thanks a ton. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Bad state: Bad state: Invalid binding 'role' on 'UsersController.getUsers': Parameter type does not implement static parse method.
**** Stacktrace
The text was updated successfully, but these errors were encountered: