Why I'm Getting this error while Using Flutter Post Method

Issue

I’m trying to create a login system Using Post Method But I’m getting Error

I tested it with postman using it works like a charm.

I Tried Other Things but can’t be able to solve this error. if someone here can solve my problem it would be grateful.

Dart Code

 Future userLogin() async {
    // var data = {'username': "admin", 'password': "admin12345"};
    var response = await http
        .post(loginAPI, body: {'username': 'admin', 'password': 'admin12345'});
    var message = json.decode(response.body);
    print(message);
  }

Login Script I Used to shore data.

<?php

 if($_SERVER['REQUEST_METHOD']=='POST'){

 include 'DatabaseConfig.php';
 
 $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
 
 $username = $_POST['username'];
 $password = $_POST['password'];
 
 $Sql_Query = "select * from UserDetails where username = '$username' and password = '$password'";
 
 $check = mysqli_fetch_array(mysqli_query($con,$Sql_Query));
 
 if(isset($check)){
 
 echo "Success";
 }
 else{
 echo "Invalid";
 }
 
 }else{
 echo "Again";
 }
mysqli_close($con);

?>

Whenever I try to login I’m getting this error.

E/flutter ( 3935): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter ( 3935): Success
E/flutter ( 3935): ^
E/flutter ( 3935):
E/flutter ( 3935): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1404:5)
E/flutter ( 3935): #1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1271:9)
E/flutter ( 3935): #2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:936:22)
E/flutter ( 3935): #3      _parseJson (dart:convert-patch/convert_patch.dart:40:10)
E/flutter ( 3935): #4      JsonDecoder.convert (dart:convert/json.dart:505:36)
E/flutter ( 3935): #5      JsonCodec.decode (dart:convert/json.dart:156:41)
E/flutter ( 3935): #6      _BodyContainerState.userLogin
package:wish_coin/Screens/LoginScreen.dart:104
E/flutter ( 3935): <asynchronous suspension>
E/flutter ( 3935): #7      _BodyContainerState.loginButtonPressed.<anonymous closure>

Solution

I Fix this issue.

<?php

 include 'DatabaseConfig.php';
 
 $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
 
 $username = $_POST['username'];
 $password = $_POST['password'];
 
 $Sql_Query = "select * from UserDetails where username = '$username' and password = '$password'";
 
 $check = mysqli_fetch_array(mysqli_query($con,$Sql_Query));
 
 if(isset($check)){
 
 echo "Success";
 }
 else{
 echo "Invalid";

 }else{
 echo "Again";
 }
mysqli_close($con);

?>

Answered By – user14581222

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *