使用“|”解析令牌时出现格式异常在 jsonDecode()

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

使用“|”传递令牌时出现格式异常在 jsonDecode() 里面。

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>;
  }

当您使用 SharedPreferences 时,您直接检索一个字符串,因此您不需要解码 json 值。