I am working in flutter app that connect to the phpMyAdmin. I have to check the phone number if it's found in the database or not found and it's works and the result "found" or not return into the debug console successfully. My problem is how can i connect the flutter to the result from php? if it's found i want to print a message and if it not show another message.
this is the php :
<?php
include "config.php"; // call connection
$err= array();
$phone_login = $_POST["login_phone"];
try{
$stmt = $connect->prepare("SELECT * FROM profile WHERE phone = ?");
$stmt->execute(array($phone_login));
$count = $stmt->rowCount();
}catch(PDOException $e){
echo $e-> getMessage();}
if($count > 0){
echo json_encode(array("status" => "found"));
}else{
echo json_encode(array("status" => "not found"));
}
?>
and this is the function in login.dart:
const String linklogin = "$linkServerName/checkphone.php";
JsonAPI _tst = JsonAPI();
login() async {
var response = await _tst.postRequest(linklogin, {
"login_phone": mobileNumTF_login.text,
});
if (response['status'] == "found") {
print("found");
} else {
print("not found");
}
}
How can i do an action in flutter app if i received the found and not found?
CodePudding user response:
You can use a FutureBuilder, pass the login() future and display a Text() widget as follows:
FutureBuilder<String>(
future: login(),
builder: (
BuildContext context,
AsyncSnapshot<String> snapshot,
) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
} else if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return const Text('Error');
} else if (snapshot.hasData) {
return Text(
snapshot.data,
style: const TextStyle(color: Colors.cyan, fontSize: 36)
);
} else {
return const Text('Empty data');
}
} else {
return Text('State: ${snapshot.connectionState}');
}
},
),
CodePudding user response:
if (response.statusCode == 200){
}else {
print("______##################################_______");
print(
'Request failed with status: ${response.statusCode}.');
print("______##################################_______");
}