user13268409
user13268409

Reputation:

How to solve [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FormatException: Unexpected character (at character 1)

I am doing a login function in Flutter. I want to use the error message shown at the alert message. Now I am getting this error:

E/flutter ( 2690): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter ( 2690): User does not exist
E/flutter ( 2690): ^

Here is my code:

Future login(String url, {Map body}) async {
    return http.post(url, body: body).then((http.Response response) {
        print(response.body);

        if(json.decode(response.body) == "User does not exist") {
            print("wrong email");
        }
    });
}

Does anyone know how to solve this error?

Upvotes: 0

Views: 4210

Answers (2)

mkrieger1
mkrieger1

Reputation: 23139

The string User does not exist is not valid JSON. This is what the error message is telling you.

To be valid JSON, it would have to contain quotes.

There are two options:

  1. Change the server to encode the response as JSON.

  2. Do not try to decode the response as JSON.

    Simply use:

    if(response.body == "User does not exist")
    

Upvotes: 1

GJJ2019
GJJ2019

Reputation: 5172

try using this :

Future login(String url, {Map body}) async {
  var response = await http.post(url, body: body)

  if(json.decode(response.body) == "User does not exist") {
    print("wrong email");
   // return something here :)
  } else{
    return json.decode(response.body);
  }
}

Upvotes: 0

Related Questions