0

I try to navigate to WelcomeScreen after the user successfully signup, but it does not do so although it works fine with Login screen and I receive this error :

I/flutter ( 5046): FormatException: Unexpected character (at character 1) I/flutter ( 5046): user added successfully I/flutter ( 5046): ^'

here is my code:

Future signup(BuildContext cont) async{
  var url = "http://10.0.2.2:8080/localconnect/signup.php";
  var response = await http.post(url, body: {
    'username' : username.text,
    'password' : password.text,
  });

  try {
    var data = jsonDecode(response.body);
    Navigator.push(cont, MaterialPageRoute(builder: (context)=>WelcomeScreen()));
  }catch(e){
    print(e);
  }
}
Alex Ali
  • 1,339
  • 5
  • 23
  • 34
  • [I found the answer here :](https://stackoverflow.com/a/65552861/4540284) var data = await json.decode(json.encode(response.body)); – Alex Ali Jul 03 '22 at 06:43

2 Answers2

1

You have to put condition on statuscode of response, and on the value of response too like,

Future signup(BuildContext cont) async{
var url = "http://10.0.2.2:8080/localconnect/signup.php";
var response = await http.post(url, body: {
'username' : username.text,
'password' : password.text,
});
try {
var data = jsonDecode(response.body);
if(response.statuscode==200){
if(data['status']) {//status is bool from APi.
Navigator.push(cont, MaterialPageRoute(builder: 
(context)=>WelcomeScreen()));
}
}
}catch(e){
print(e);
}
}
Muhammad Umair
  • 274
  • 3
  • 13
0

You have to send data like this : use jsonEncode for body. As you are getting response in string means thats error thats why you are getting like that. Or you can share api response so, can get idea.

 var response = await http.post(url, body: jsonEncode({
    'username' : username.text,
    'password' : password.text,
  }));
Hardik Mehta
  • 2,195
  • 1
  • 11
  • 14