findToken static method

dynamic findToken(
  1. Map json,
  2. String tokenKey
)

Finds the token inside a json map using the tokenKey. tokenKey can be a tree path using / as node delimiter.

Implementation

static dynamic findToken(Map json, String tokenKey) {
  if (json.isEmpty || tokenKey.isEmpty) return null;

  var tokenKeys = tokenKey.split('/');

  dynamic token = findKeyValue(json, [tokenKeys.removeAt(0)], true);
  if (token == null) return null;

  for (var k in tokenKeys) {
    if (token is Map) {
      token = findKeyValue(token, [k], true);
    } else if (token is List && isInt(k)) {
      var idx = parseInt(k);
      token = token[idx!];
    } else {
      token = null;
    }

    if (token == null) return null;
  }

  if (token is String || token is num) {
    return token;
  }

  return null;
}