未處理的例外:FormatException:意外的字元(在字元 1 處) E/flutter ( 6084): <br /> E/flutter ( 6084): ^
P粉891237912
P粉891237912 2024-03-26 09:13:45
0
1
488

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

登入頁面 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,
        );
      }
    }}

控制台:

E/flutter (6084): [錯誤:flutter/lib/ui/ui_dart_state.cc(198)] 未處理的異常:FormatException:意外的字元(位於 字元 1) E/顫振(6084):

# E/顫動(6084):^ E/顫振(6084): E/顫振(6084): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5) E/顫振(6084): #1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1250:9) E/顫振(6084): #2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:915:22) E/顫振(6084): #3 _parseJson (dart:convert-patch/convert_patch.dart:35:10) E/顫振(6084): #4 JsonDecoder.convert (dart:convert/json.dart:612:36) E/顫振(6084): #5 JsonCodec.decode (dart:convert/json.dart:216:41) E/顫振(6084): #6 jsonDecode (dart:convert/json.dart:155:10) E/顫振(6084): #7 Login.login (package:mes_v1/pages/Authentification/login.dart:25:18) E/顫振(6084): E/顫振(6084):

P粉891237912
P粉891237912

全部回覆(1)
P粉244730625

您的程式碼看起來確實容易出錯,但重點關注您的問題:異常看起來很清楚,您的回應解析正在中斷。 (我還想說,你應該用 try/catch 包裝你的程式碼,以防止任何程式碼破壞和正確捕獲問題)。

讓我們解決您的問題:

var data = jsonDecode(response.body);

這會將您的字串(response.body) 轉換為json,這表示dataMap# 或List # (其中動態是Map 或另一個巢狀清單),因此以下內容沒有任何意義

if(資料==「成功」){

現在,讓我們來看看您的 php 程式碼:

echo json_encode("成功");

#我不是 php 專家,但從文件來看它應該像下面這樣使用:

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

現在讓我們回到您的 dart 程式碼:

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) {
   ...
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板