flutter - 如果用户登录正确颤动,如何从表中选择所有数据
问题描述
我尝试将用户登录页面设置到我的应用程序。页面现在可以工作,但我也需要做同样的事情。如果用户登录正确,我需要选择该用户的所有数据。
我使用此代码登录:
var url = 'http://xxxxxxxx/login_user.php';
var data = {'email': email, 'password' : password};
var response = await http.post(url, body: json.encode(data));
var message = jsonDecode(response.body);
print("tapped ${data}");
if(message == 'Login Matched')
{
setState(() {
visible = false;
});
Navigator.push(context, MaterialPageRoute(builder: (context) => mainpage(email : emailController.text)));
}else{
setState(() {
visible = false;
});
<?php
include 'connt.php';
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$email = $obj['email'];
$password = $obj['password'];
$loginQuery = "select * from user_registration where email = '$email' and password = '$password' ";
$check = mysqli_fetch_array(mysqli_query($con,$loginQuery));
if(isset($check)){
$onLoginSuccess = 'Login Matched';
$SuccessMSG = json_encode($onLoginSuccess);
echo $SuccessMSG ;
}
else{
$InvalidMSG = 'Invalid Username or Password Please Try Again' ;
$InvalidMSGJSon = json_encode($InvalidMSG);
echo $InvalidMSGJSon ;
}
mysqli_close($con);
?>
我的表中有不同的字段,例如姓氏、年龄、告诉..'
我需要选择所有字段。现在,如果我尝试通过此代码查看数据:
print("tapped ${data}");
只是我有电子邮件和密码。所以如果有人知道我该怎么做,请帮助我。
完整代码
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:onefrist/main.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'MyPreferences.dart';
import 'main.dart';
import 'Registration.dart';
void main() => runApp(loginpage());
var id;
class loginpage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.amber,
title: Text('User Login Form')),
body: Center(
child: LoginUser()
)
)
);
}
}
class LoginUser extends StatefulWidget {
LoginUserState createState() => LoginUserState();
}
class LoginUserState extends State <LoginUser>{
MyPreferences _myPreferences = MyPreferences();
@override
void initState() {
// TODO: implement initState
super.initState();
_myPreferences.init().then((value) {
setState(() {
_myPreferences = value;
check_if_already_login();
});
});
}
void check_if_already_login() async {
if (_myPreferences.password !=null && _myPreferences.user !=null) {
Navigator.pushReplacement(
context, new MaterialPageRoute(builder: (context) =>mainpage ()));
}else{
}
}
bool visible = false ;
Future userLogin() async{
setState(() {
visible = true ;
});
final emailController = TextEditingController(text: _myPreferences.user);
final passwordController = TextEditingController(text: _myPreferences.password);
// final idController = TextEditingController();
// Getting value from Controller
String email = emailController.text;
String password = passwordController.text;
// String id = idController.text;
if(email == '' || password == '')
{
// Put your code here which you want to execute when Text Field is Empty.
print('Text Field is empty, Please Fill All Data');
}else{
var url = 'http://192.168.8.105/login_user.php';
var data = {'email': email, 'password' : password};
var response = await http.post(url, body: json.encode(data));
var message = jsonDecode(response.body);
print("tapped ${data}");
if(message == 'Login Matched')
{
setState(() {
visible = false;
});
Navigator.push(context, MaterialPageRoute(builder: (context) => mainpage(email : emailController.text)));
}else{
setState(() {
visible = false;
});
// Showing Alert Dialog with Response JSON Message.
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text(message),
actions: <Widget>[
FlatButton(
child: new Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);}
}
}
@override
Widget build(BuildContext context) {
final emailController = TextEditingController(text: _myPreferences.user);
final passwordController = TextEditingController(text: _myPreferences.password);
final idController = TextEditingController();
return Scaffold(
body: SingleChildScrollView(
child: Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(12.0),
child: Text('User Login Form',
style: TextStyle(fontSize: 21))),
Divider(),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: emailController,
onChanged: (value) {
_myPreferences.user = value;
_myPreferences.commit();
},
autocorrect: true,
decoration: InputDecoration(hintText: 'Enter Your Email Here'),
)
),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: passwordController,
onChanged: (value) {
_myPreferences.password = value;
_myPreferences.commit();
},
autocorrect: true,
obscureText: true,
decoration: InputDecoration(hintText: 'Enter Your Password Here'),
)
),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: idController,
autocorrect: true,
decoration: InputDecoration(hintText: 'Enter Your Password Here'),
)
),
RaisedButton(
onPressed: userLogin,
color: Colors.green,
textColor: Colors.white,
padding: EdgeInsets.fromLTRB(9, 9, 9, 9),
child: Text('Click Here To Login'),
),
FlatButton(
color: Colors.blue,
textColor: Colors.white,
disabledColor: Colors.grey,
disabledTextColor: Colors.black,
padding: EdgeInsets.all(8.0),
splashColor: Colors.blueAccent,
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => RegisterUser()
),);
},
child: Text(
"Reistraion page",
style: TextStyle(fontSize: 20.0),
),
),
Visibility(
visible: visible,
child: Container(
margin: EdgeInsets.only(bottom: 30),
child: CircularProgressIndicator()
)
),
],
),
)));
}
}
解决方案
是的。如果您打印,则只能有电子邮件和密码data
。
考虑print(message)
。
这可以显示从服务器接收到的数据!同样在您的服务器中,考虑将所有数据作为响应发送。
$check = mysqli_fetch_array(mysqli_query($con,$loginQuery));
if($check){
$check['password'] = '';
// Make sure you are not returning the password for security reasons!
$check['result'] = 'Login Matched';
$SuccessMSG = json_encode($check);
echo $SuccessMSG ;
}
else{
$InvalidMSG= array("result"=>"Invalid Username or Password Please Try Again");
$InvalidMSGJSon = json_encode($InvalidMSG);
echo $InvalidMSGJSon ;
}
在您的飞镖代码中,您可以简单地执行此操作。
if(message['result'] == 'Login Matched')
{
// Do Something
}else{
// Do Something
}
推荐阅读
- java - springframework 版本 5.0.5.RELEASE 引起:java.lang.NoClassDefFoundError: org/springframework/core/task/AsyncListenableTaskExecutor
- c# - 如何解决 Visual Studio Code 中未使用的代码错误?
- c# - 如何在 .NetCore 中使用 Selenium C# 记录测试执行
- apache-flink - Flink 是否支持 BroadcastState 的 State TTL?
- sql - BigQuery:数字数据类型:不能存储超过 19 位数字
- hibernate - 需要在加入表格的标准中等同于多个属性
- spring-boot - 如何解析json spring boot的一些字段
- scala - 如何使用 Spark 处理非结构化文本文件
- r - 在R中将数值变量转换为时间变量
- r - 如何使用for循环来简化代码?