Home > database >  Exception has occurred. FormatException (FormatException: Unexpected character (at character 1) <
Exception has occurred. FormatException (FormatException: Unexpected character (at character 1) <

Time:01-03

Need Help, pop up error 'Exception has occurred. FormatException (FormatException: Unexpected character (at character 1)

^ )'

Future<Map<String, dynamic>> deleteProduct(String productId) async {
    String url = 'http://127.0.0.1:8000/api/products'   productId;
    var response = await http.delete(Uri.parse(url));
    return json.decode(response.body);
  }

I have a problem with 'return json.decode(response.body);'

 Never fail(int position, [String? message]) {
    if (message == null) {
      message = "Unexpected character";
      if (position == chunkEnd) message = "Unexpected end of input";
    }
    throw new FormatException(message, chunk, position);
  }
}

and it exits a code window like this when I run it in the convert_patch.dart file

Need Help, pop up error 'Exception has occurred. FormatException (FormatException: Unexpected character (at character 1)

CodePudding user response:

Your call to http.delete is returning an error and not the json you expect, hence json.decode fails with a format error. When you make url calls you need to test the status of the response before trying to process it. Status 200 should indicate success.

Regarding what is causing the error, I would guess your url needs a closing '/' or '=' character before you concatenate the productId.

  • Related