Reputation: 31
I am trying to get some information from a database which I do get eventually, but my if conditions are checked first before getting the data and prints the data after completing the checking of the if conditions, even though I have used await to wait for the data to arrive and then continue.
Future reg() async {
getData().then((value) async {
print(value["serverIP"]);
print(value["port"]);
print(value["passwordMain"]);
Dio dio = Dio();
Response response = await dio.get(
'http://${value["serverIP"]}:${value["port"]}/${value["passwordMain"]}/reg/${controllerEmail.text}/${controllerPassword.text}/${controllerUsername.text}');
print(response.data);
return response;
});
ElevatedButton(
onPressed: () async {
if (!controllerEmail.text.endsWith("@gmail.com") &
!controllerEmail.text.endsWith("@gmail.com ") &
!controllerEmail.text.endsWith("@email.com") &
!controllerEmail.text.endsWith("@email.com ") &
!controllerEmail.text.endsWith("@hotmail.com") &
!controllerEmail.text.endsWith("@hotmail.com ")) {
if (controllerEmail.text.endsWith(" ")) {
controllerEmail.text =
controllerEmail.text.replaceAll(" ", "");
}
showErrorDialog(context, 'Unknown Email Address',
'Try Changing the Email to one of the Providers we Support.');
} else if ((controllerPassword.text !=
controllerRePassword.text) |
controllerPassword.text.isEmpty) {
showErrorDialog(context, 'Passwords Do not Match/Empty',
'Please Re-Type your Passwords as they do not Match, or are Empty');
} else {
var response = await reg();
if (response != null) {
if (response.data == "done") {
showErrorDialog(context, "Done",
"Your Account has been Created, please Log in");
} else if (response.data == "key") {
showErrorDialog(
context,
"Incorrect API Key/Main Server Password",
"The API Key (Main Server Password) is Incorrect. Kindly, Ensure the Key.");
} else if (response.data == "email") {
showErrorDialog(context, "Account Already Exists",
"An Account already exists with this Email");
} else if (response.data == "username") {
showErrorDialog(context, "Account Already Exists",
"An Account already exists with this Username");
}
}
}
},
child: const Text("Sign Up"),
),
Upvotes: 0
Views: 151
Reputation: 31
You're missing a return
in your reg()
function. Add one before your getData()
call like this:
Future reg() async {
try {
return getData().then((value) async {
Dio dio = Dio();
Response response = await dio.get(
'http://${value["serverIP"]}:${value["port"]}/${value["passwordMain"]}/reg/${controllerEmail.text}/${controllerPassword.text}/${controllerUsername.text}');
return response;
});
} catch (e) {}
}
Now the function should be properly awaited because it is now returning a promise instead of nothing.
Alternatively, you might prefer to rewrite it using more async/await for easier comprehension, like this:
Future reg() async {
try {
const value = await getData();
Dio dio = Dio();
Response response = await dio.get(
'http://${value["serverIP"]}:${value["port"]}/${value["passwordMain"]}/reg/${controllerEmail.text}/${controllerPassword.text}/${controllerUsername.text}');
return response;
} catch (e) {}
}
Credit: https://stackoverflow.com/a/74238420/13909069
Upvotes: 1