android – I’m having an issue with my flutter registration with mysql and php

0
322


I’m new to flutter. The registration is not working with mysql and php. here is my code.

      Future register() async {
var url = "http://192.168.0.142/register.php";
var response = await http.post(Uri.parse(url),
    // header('Content-Type: application/json; charset=utf-8'),
    body: {
  "username": user.text,
  "password": pass.text,
});

if(response.body.isNotEmpty) {
  debugPrint("not empty ${user.text}");

  json.decode(response.body);
}
else
{
  debugPrint("empty ${user.text}");

}
var data = json.decode(response.body);
if (data == "Error") {
  // FlutterToast(context).showToast(
  //     child: Text(
  //       'User allready exit!',
  //       style: TextStyle(fontSize: 25, color: Colors.red),
  //     ));


  print("already");

} else {
  // FlutterToast(context).showToast(
  //     child: Text('Registration Successful',
  //         style: TextStyle(fontSize: 25, color: Colors.green)));
  // Navigator.push(context, MaterialPageRoute(builder: (context)=>DashBoard(),),);
  print("seccussfull");
}

}

here is my registration.php file

     <?php
    $db = mysqli_connect('localhost','root','','userdata');
    if (!$db) {
        echo "Database connection faild";
    }
    else
    {
    
    echo "Success";

     }

    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "SELECT username FROM login WHERE username="".$username.""";

    $result = mysqli_query($db,$sql);
    $count = mysqli_num_rows($result);

    if ($count == 1) {
        echo json_encode("Error");
    }else{
        $insert = "INSERT INTO          login(username,password)VALUES('".$username."','".$password."')";
        $query = mysqli_query($db,$insert);
        if ($query) {
            echo json_encode("Success");
        }
    }

?>

I’m getting this error

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: FormatException: Unexpected character (at character 1)
Success
^

#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1405:5)
#1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1272:9)
#2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:937:22)
#3      _parseJson (dart:convert-patch/convert_patch.dart:40:10)
#4      JsonDecoder.convert (dart:convert/json.dart:612:36)
#5      JsonCodec.decode (dart:convert/json.dart:216:41)
#6      _RegisterState.register (package:employeessql/register.dart:31:12)
<asynchronous suspension>

The login is working by adding manual data. But the registration is not working.

I have tried every way. Please help. It is related with json.decode. Also I want to know how I can update the data according to current user. Any help would be appreciated