我正在尝试使用Rest创建一个登录表单,但是我总是会出错。要登录,API需要3个可变项:电子邮件、密码和设备。
这是我的登录功能
String email;
String password;
String device="mobile";
TextEditingController passwordcontroller=TextEditingController();
TextEditingController emailcontroller=TextEditingController();
loginfunction(String email, String password, String device)async{
// Map data={"email":email,
// "password":password,"device":device};
var jsonData;
SharedPreferences sharedPreferences=await SharedPreferences.getInstance();
var response= await http.post('https://f.nz/api/auth/Login',headers:{
"key": "Client-Version",
"value": "{{client_version}}-1",
"type": "text"
},
body: {
"email": email,
"password": password,
"device": device,
});
// return response.data;
if (response.statusCode==200){
jsonData= json.decode(response.body);
setState(() {
sharedPreferences.setString("token", jsonData["token"]);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
OtpPage()));
});
}
else{
print(response.body);
print(response.statusCode);
print(email);
print(device);
}
}这里是在填写电子邮件和密码字段后调用函数的地方。
GestureDetector(
onTap: () async {
try{
await loginfunction(email, password,device);
}
catch(e){
setState(() {
error=e.message;
});
}
},
child: Container(
padding: EdgeInsets.fromLTRB(.0, 0.0, 0.0, 0.0),
height: 35.0,
width: 150.0,
child: Material(
borderRadius: BorderRadius.circular(40.0),
color: Color(0xffa6ce38),
elevation: 7.0,
child: Center(
child: Text(
"Sign in",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 16.0, color: Colors.white),
),
),
),
),
),每次我运行代码并尝试在其中签名时,这都是终端中的错误
D/libc( 7365):getaddrinfo: mtk hostname=fhm.soop.co.nz;servname=(空);netid=0;mark=0 D/libc( 7365):getaddrinfo( app_uid:10366 D/libc( 7365):getaddrinfo() uid支柱:d/libc( 7365):getaddrinfo() getuid():10366 D/libc( 7365):getaddrinfo: mtk ai_addrlen=0;ai_canonname=(空);ai_flags=1024;ai_family=0 D/libc( 7365):getaddrinfo: fhm.soop.co.nz从代理gai_error =0i/gai_error获得结果( 7365):I/颤振( 7365):遇到一个PHP错误:I/颤振( 7365):I/颤振( 7365):
严重程度:通知
I/颤振( 7365):
信息:尝试获取非对象的属性“电子邮件”。
I/颤振( 7365):
文件名: auth/Login.php
I/颤振( 7365):
线路号码: 43
I/颤振( 7365):I/颤振( 7365):I/颤振( 7365):
回溯:
I/颤振( 7365):
I/颤振( 7365):
I/颤振( 7365):
I/颤振( 7365):
I/颤振( 7365):
I/颤振( 7365):
I/颤振( 7365):I/颤振( 7365):文件: C:\xampp\htdocs\api\application\controllers\auth\Login.php
I/颤振( 7365):行: 43
I/颤振( 7365):功能:_error_handler
I/颤振( 7365):I/颤振( 7365):
I/颤振( 7365):
I/颤振( 7365):
I/颤振( 7365):I/颤振( 7365):文件: C:\xampp\htdocs\api\application\libraries\REST_Controller.php
I/颤振( 7365):行: 708
I/颤振( 7365):功能: index_post
I/颤振( 7365):I/颤振( 7365):
I/颤振( 7365):
I/颤振( 7365):
I/颤振( 7365):
I/颤振( 7365):
I/颤振( 7365):I/颤振( 7365):文件: C:\xampp\htdocs\api\index.php
I/颤振( 7365):行: 308
I/颤振( 7365):功能: require_once
I/颤振( 7365):I/颤振( 7365):
I/颤振( 7365):
I/颤振( 7365):PHP出现错误I/颤振( 7365):I/颤振( 7365):
严重程度:通知
I/颤振( 7365):
消息:试图获取非obj I/flutter ( 7365):400 I/flutter ( 7365):email@email.com I/flutter (7365)的属性“密码”:移动
发布于 2021-04-13 23:26:03
如果您使用的电子邮件不存在于您的数据库中,这是您的php服务器的结果。我看到你的错误,email@email.com说。您的服务器正在查找用户,但没有找到它,并返回null。或者您可能在服务器端对数据进行错误的解析。我会先检查一下。
你的错误还说
Message: Trying to get property 'email' of non-object
I/flutter ( 7365):
Filename: auth/Login.php
message: A PHP Error was encountered所以,这几乎是一个显而易见的问题,只是要确保您确实将您的姓名和密码从TextEditingControllers传递到您的POST请求。
为了验证这一理论,在请求中硬编码您的凭据,看看错误是否仍然存在,并且没有改变,那么一定要转到php代码中。
body: {
"email": 'email@gmail.com',
"password": 'youSuperStrongpassword124234',
"device": device,
});此外,你应该jsonEncode你的帖子请求的身体。
https://stackoverflow.com/questions/67083517
复制相似问题