我有這個代碼:
APICalls ac = APICalls();
String getCurrentUser() {
return ac.getCurrentUser();
}
bool pass = false;
void postPassword(String idProfile) async {
final response = await ac.postItem("/v1/users/:0/pw", [
idProfile
], {
"old": oldPasswordController.text,
"new": newPassword1Controller.text
});
if (response.statusCode >= 200 && response.statusCode < 300) {
print("estoy en el if");
pass = true;
String accessToken = json.decode(response.body)['access_token'];
String userID = json.decode(response.body)['id'];
String refreshToken = json.decode(response.body)['refresh_token'];
ac.initialize(userID, accessToken, refreshToken, true);
} else if (response.statusCode == 400) {
print("estoy en el else if");
pass = false;
} else {
print("estoy en el else");
}
}
bool correctChange() {
postPassword(getCurrentUser());
return pass;
}
我在那里稱它為:
: (!correctChange())
? showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Error'),
content: const Text(
'Incorrect old password'),
actions: [
TextButton(
child: const Text('OK'),
onPressed: () =>
Navigator.pop(
context),
)
]))
: showDialog(
context: context,
builder: (context) => AlertDialog(
title:
const Text('Correct'),
content: const Text(
'Password changed correctly'),
actions: [
TextButton(
child: const Text('OK'),
onPressed: () =>
Navigator.pop(
context),
)
]));
上面的代碼是我尋找其他條件的部分。但是,這里是我呼叫異步函式的地方。我不能將所有內容都放在 FutureBuilder 中,因為我需要在查找其他內容時進行呼叫。我怎么能做到?
uj5u.com熱心網友回復:
試試這個:
Future<bool> postPassword(String idProfile) async {
bool pass = false; // this is moved INSIDE !
// set pass to true or false accordingly in if-else-clauses
return pass;
}
Future<bool> correctChange() async {
return await postPassword(getCurrentUser());
}
blaBlaBla() async {
// ... your code
: await !correctChange()
// ... more of your code
}
uj5u.com熱心網友回復:
使用 await 關鍵字呼叫任何異步函式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/479976.html
