php file:
$email=$_POST['email']; $passworda=$_POST['passworda']; $sql="SELECT * FROM user WHERE email='".$email."'AND passworda='".$password."' "; $result=mysqli_query($db,$sql); $count=mysqli_num_rows($result); if($count>=1){ echo json_encode("success"); } else { echo json_encode("error"); }
Login page Flutter:
class Login extends StatelessWidget { TextEditingController email = TextEditingController(); TextEditingController password = TextEditingController(); Future login(BuildContext cont) async { if (email.text == "" || password.text == "") { Fluttertoast.showToast( msg: "please complete!", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER, fontSize: 16.0, ); } else { var url = "http://192.168.43.150/v1_flutter/lib/php/connection.php"; var response = await http.post(Uri.parse(url), body: { "email": email.text, "pass": password.text, }, headers: {"Accept":"applicarion/json"}); var data = jsonDecode(response.body); if (data == "success") { Navigator.pop(cont); Navigator.pushNamed(cont, "/registre"); } else { Fluttertoast.showToast( msg: "The user and password does not exist!", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER, fontSize: 16.0, ); } }}
Console:
E/flutter (6084): [Error: flutter/lib/ui/ui_dart_state.cc(198)] Unhandled exception: FormatException: Unexpected character (at Character 1) E/Flutter (6084):
E/Flutter(6084):^ E/Flutter(6084): E/Flutter(6084): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5) E/Flutter(6084): #1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1250:9) E/Flutter(6084): #2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:915:22) E/Flutter(6084): #3 _parseJson (dart:convert-patch/convert_patch.dart:35:10) E/Flutter (6084): #4 JsonDecoder.convert (dart:convert/json.dart:612:36) E/Flutter (6084): #5 JsonCodec.decode (dart:convert/json.dart:216:41) E/Flutter (6084): #6 jsonDecode (dart:convert/json.dart:155:10) E/Flutter (6084): #7 Login.login (package:mes_v1/pages/Authentification/login.dart:25:18) E/Flutter(6084): E/Flutter(6084):
Your code does look error-prone, but focusing on your question: the exception looks clear and your response parsing is breaking. (I would also say that you should wrap your code with try/catch to prevent any code breaking and catch issues properly).
Let us solve your problem:
var data = jsonDecode(response.body);
This will convert your string (response.body) to json, which means
data
is aMap
orList (where dynamic is a Map or another nested list), so the following doesn't make any sense
if(data=="success"){
Now, let's take a look at your php code:
echo json_encode("Success");
I'm not a php expert, but from the documentation it should be used like this:
Now let’s get back to your dart code: