使用 API 进行 Flutter 注册 [英] Flutter Registration with API
问题描述
我可以使用我的 API 连接成功登录,也可以在我的应用中显示数据.
I successfully can login with my API connection also can display the data in my app.
我按照注册一样的方法在API中发送数据以便我可以登录.
I follow the same method for the registration to send data in API So that I can login.
但是注册并没有发生,尽管它没有显示任何错误.我的 SnackBar 显示 您已经注册
.但是当我尝试使用我在注册表单中提供的数据登录时,它显示未找到用户(明显的原因注册过程没有执行).我的 API 模型有问题吗?
But the registration is not happening, although it didn't show any error. My SnackBar showing You are already registered
. But When I try to login with the data which I provided in registration form, its showing user not found( Obvious reason registration process didn't perform). Is there any problem with my API MODEL?
这是我的参数
{
"Email":"qwer@gmail.com",
"Mobile":"1237891",
"Password":"9991",
"RetypePassword":"9991"
}
这是 Json 响应
{
"Status": "1",
"Message": "You are registered successfully",
"UserData": {
"Name": "qwer@gmail.com",
"EncUserId": "IS0QOCrLby8Ft1kbkzn/mg=="
}
}
之后我创建了一个注册表单,当用户点击Register
按钮时,它会运行我的API函数registrationOfuser()
After that I created a registration form, when user click the Register
button it's run my API function registrationOfuser()
Future <void> registrationOfuser(String email, contact, pass,conpass) async{
var jsonResponse ;
Map data = {
'Email': email,
'Mobile': contact,
'Password': pass,
'RetypePassword': conpass,
};
print(data);
String body = json.encode(data);
var url = 'http://myurl/home/djkjkfjkjfwkjfkwjkjfkjwjfkwjfkwf';
var response = await http.post(
url,
body: body,
headers: {
"Content-Type": "application/json",
"accept": "application/json",
"Access-Control-Allow-Origin": "*"
},
);
print(response.body);
print(response.statusCode);
if (response.statusCode == 200) {
jsonResponse = json.decode(response.body.toString());
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content:Text(" ${jsonResponse['Message']}"))) ;
//Or put here your next screen using Navigator.push() method
print('success');
} else {
print('error');
}
}
这是我的模型
class RegistrationApiResponse {
RegistrationApiResponse({
required this.status,
required this.message,
required this.userData,
});
String status;
String message;
//UserData userData;
UserData? userData;
factory RegistrationApiResponse.fromJson(Map<String, dynamic> json) => RegistrationApiResponse(
status: json["Status"],
message: json["Message"],
userData: json["UserData"] == null? null:UserData.fromJson(json["UserData"] as Map<String, dynamic>),
}
class UserData {
UserData({
required this.name,
required this.encUserId,
});
String name;
String encUserId;
factory UserData.fromJson(Map<String, dynamic> json) => UserData(
name: json["Name"],
encUserId: json["EncUserId"],
);
}
推荐答案
尝试下面的代码帮助:
创建 TextEditingController
final TextEditingController email = new TextEditingController();
final TextEditingController contact = new TextEditingController();
final TextEditingController password = new TextEditingController();
final TextEditingController conpassword = new TextEditingController();
创建一个注册函数
register(String email, contact, pass,conpass) async {
Map data = {
'Email': email,
'Mobile': contact,
'Password': pass,
'RetypePassword': conpass,
};
print(data);
String body = json.encode(data);
var url = 'Your url here';
var response = await http.post(
url,
body: body,
headers: {
"Content-Type": "application/json",
"accept": "application/json",
"Access-Control-Allow-Origin": "*"
},
);
print(response.body);
print(response.statusCode);
if (response.statusCode == 200) {
//Or put here your next screen using Navigator.push() method
print('success');
} else {
print('error');
}
}
创建按钮
ElevatedButton(
child:Text('Register'),
onPressed:(){
register(email.text, contact.text, password.text, conpassword.text);
},
),
这篇关于使用 API 进行 Flutter 注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!