Flutter Json FormatException


In my application, i’m trying to retrieve the data from shared preferences and pass that value to the provider. How ever im getting a exception

FormatException (FormatException: Unexpected character (at character 2)
{token: eyJh...

The function to get and pass the value to provider is as follows

   Future<void> insertUsertolocal() async {
        final storage = new LocalStorage();
        final _user = await storage.getValue(Constants.USER); // retrieves data from key
        User user = User.fromJson(jsonDecode(_user)); // receives FortmatException

The json data is stored in shared preferences as string

_localStorage.setValue(Constants.USER, user.toString());

How can i resolve this issue??


You should use jsonEncode() in

Check the doc here: https://flutter.dev/docs/development/data-and-backend/json#serializing-json-manually-using-dartconvert

Answered By – Carlos Nantes

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *