Home > Mobile >  Flutter : Navigate to Welcome Screen after Successful Signup
Flutter : Navigate to Welcome Screen after Successful Signup

Time:07-03

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);
  }
}

CodePudding user response:

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,
  }));

CodePudding user response:

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);
}
}
  • Related