urlBase64Decode function

String urlBase64Decode(
  1. String str
)

Implementation

String urlBase64Decode(String str) {
  // From https://jwt.io/js/jwt.js
  String output = str.replaceAll(r'/-/g', '+').replaceAll(r'/_/g', '/');
  switch (output.length % 4) {
    case 0:
      break;
    case 2:
      output += '==';
      break;
    case 3:
      output += '=';
      break;
    default:
      throw 'Illegal base64url string!';
  }

  // polifyll https://github.com/davidchambers/Base64.js
  final result = base64Decode(output);
  try {
    return Uri.decodeComponent(utf8.decode(result));
  } catch (e) {
    return String.fromCharCodes(result);
  }
}