tryParse static method
Tries to parse a JWT access token string, returning error info if invalid.
This method provides safe token parsing that returns both success and error information without throwing exceptions.
token The JWT token string to parse.
Returns a record with either the parsed AccessToken or error information.
- data: The parsed AccessToken if successful, null if failed
- error: The error that occurred during parsing, null if successful
Example
final result = AccessToken.tryParse(tokenString);
if (result.data != null) {
print('Token is valid for user: ${result.data!.userId}');
} else {
print('Parse failed: ${result.error}');
}
Implementation
static ({AccessToken? data, Object? error}) tryParse(String token) {
try {
final accessToken = AccessToken.parse(token);
return (data: accessToken, error: null);
} catch (error) {
return (data: null, error: error);
}
}