'How do I correct this flutter error: Unexpected token a in JSON at position 0?
When I send data from flutter to php I am getting the error below. I am using dropdown and map to display and collect the date selection. I also have a few text boxes Error: FormatException: SyntaxError: Unexpected token a in JSON at position 0 at Object.throw_ [as throw] (http://localhost:50857/dart_sdk.js:5334:11)
Future _submitemail() async {
var apiUrl = "http://www.redhowler.com/db/signup.php";
String date = year +'-'+ month +'-'+ day;
print(date);
Map mappedData = {
'email': _email.text,
'date': date
};
http.Response response = await http.post(apiUrl, body: mappedData);
var data = jsonDecode(response.body);
print(data);
}
}
Solution 1:[1]
To solve this problem I format the body data the way it is display on the form. I capture date first, then email. So I changed the mappedData. Look at the code when I first post the problem compare to the code below.
Future _submitemail() async {
var apiUrl = "http://www.redhowler.com/db/signup.php";
String date = year +'-'+ month +'-'+ day;
print(date);
Map mappedData = {
'date': date,
'email': _email.text
};
http.Response response = await http.post(apiUrl, body: mappedData);
var data = jsonDecode(response.body);
print(data);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Cameron Browne |