Search code examples
flutterflutter-dependenciesdio

Getting Format Exception while parsing token with '|' in jsonDecode()


Getting the format exception while passing the token with '|' inside jsonDecode(). enter image description here

final SharedPreferences pref = await SharedPreferences.getInstance();
    value = pref.get("token") as String;
    if (value?.isEmpty ?? true) {
      return null;
    } else {
      final Map<String, dynamic> map = await _parseJson(value);
      return OldToken.fromJson(map);
    }

static Future<Map<String, dynamic>> _parseJson(String text) {
    return compute(_parseAndDecode, text);
  }

static Map<String, dynamic> _parseAndDecode(String response) {
    return jsonDecode(response) as Map<String, dynamic>;
  }

Solution

  • When you use SharedPreferences you retrieve directly a String, so you don't need to decode a json value.