Flutter 异步函数不同步
Flutter async function not synchronized
我有这个代码:
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 中,因为我需要在查找其他内容时进行调用。我该怎么做?
试试这个:
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
}
使用 await 关键字调用任何异步函数。
我有这个代码:
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 中,因为我需要在查找其他内容时进行调用。我该怎么做?
试试这个:
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
}
使用 await 关键字调用任何异步函数。