Pengecualian tidak dikendalikan: FormatException: Aksara tidak dijangka (pada aksara 1) E/flutter (6084): <br /> E/flutter (6084): ^
P粉891237912
P粉891237912 2024-03-26 09:13:45
0
1
456

fail php:

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

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

Konsol:

E/flutter (6084): [Ralat: flutter/lib/ui/ui_dart_state.cc(198)] Pengecualian tidak dikendalikan: FormatException: Watak tidak dijangka (at Watak 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 (pakej:mes_v1/pages/Authentication/login.dart:25:18) E/Flutter(6084): E/Flutter (6084):

P粉891237912
P粉891237912

membalas semua(1)
P粉244730625

Kod anda kelihatan mudah ralat, tetapi memfokuskan pada masalah anda: pengecualian kelihatan jelas dan penghuraian respons anda rosak. (Saya juga akan mengatakan bahawa anda harus membungkus kod anda dengan cuba/tangkap untuk mengelakkan sebarang masalah pecah kod dan menangkap dengan betul).

Biar kami selesaikan masalah anda:

var data = jsonDecode(response.body);

Ini akan menukar rentetan anda (response.body) kepada json, yang bermaksud dataMap atau List (di mana dinamik ialah Peta atau senarai bersarang lain), jadi perkara berikut tidak masuk akal

if(数据==“成功”){jika(data=="berjaya"){

Sekarang, mari lihat kod php anda:

echo json_encode("成功");echo json_encode("berjaya");

Saya bukan pakar php, tetapi dari dokumentasi ia harus digunakan seperti ini:

$response = array("result" => "Success");
echo json_encode($response);

Sekarang mari kembali kepada kod dart anda:

var success = false;
    try{

      final baseUrl = "http://192.168.43.150"; // Use final wherever you can
      final url = "$baseUrl/v1_flutter/lib/php/connection.php"; // Improving flexibility

      final body = {
        "email": email.text,
        "pass": password.text,
      }; // Decouple in variables for readability

      final headers = {"Accept":"application/json"}; // Fix typo

      final response = await http.post(Uri.parse(url), body: body, headers: headers);
      final data = jsonDecode(response.body);
      success = data["result"] == "Success";
   } catch(e) {
      print("Catched an error!");
      print(e);
      success = false;
   }

   if(success) {
   ...
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan