0

I'm making a postDetails page in Flutter and I've written a function that will list the comments for that post. It pulls data from database correctly (I'm debugging) but FormatException (FormatException: Unexpected character (in char 1) It gives error. işte kodum;

Future<void> getComments() async {
  var url = Uri.parse("http://172.30.182.148/uploads/getComments.php");
  var response = await http.post(url, body: {"post_id": widget.id});

  if (response.statusCode == 200) {
    var data = jsonDecode(response.body);
    setState(() {
      comments = List<String>.from(data['comment']);
    });
  } else {
    Fluttertoast.showToast(msg: "Yorumlar alınamadı");
  }
}
  • Most likely the body is not a valid json. Since it is PHP my guess is that there is an error or warning during the script execution ands PHP add this to the output, which makes it an invalid json. – Peter Koltai Jun 19 '23 at 13:03
  • 1
    Before the line `var data = jsonDecode(response.body);` try to do `print(response.body);` to see what you are actually getting – Ivo Jun 19 '23 at 13:07
  • writes data correctly when i do – Sıla Yıldırım Jun 19 '23 at 13:15
  • 1
    well, the error suggests otherwise. We can't help much if you don't tell us what that response it – Ivo Jun 19 '23 at 13:17

1 Answers1

-1

Please make sure that the json you received is correctly formatted (commas,quotation marks).

You can check that by printing the body of the response, right before trying to decode it, and check the result by using a json validator, like this one: https://jsonlint.com/

car_tor
  • 109
  • 4
  • It can also be that one character in the json is not supported (special characters, diacritics). The json has to be sent as contentType: "charset=utf-8" so the json receiver will also need to process it in UTF-8 – car_tor Jun 19 '23 at 16:13