【发布时间】:2021-10-04 10:42:49
【问题描述】:
当我使用 async 函数然后使用 await 关键字时,代码执行会停止直到 await 解决,还是将下一行代码(或代码块)作为 .then 并继续像往常一样执行其余部分?
Future<void> deleteProduct(String id) async {
final url = Uri.parse(
'https://my-shop-38f1d-default-rtdb.firebaseio.com/products/$id.json?auth=$authToken');
final existingProductIndex = _items.indexWhere((prod) => prod.id == id);
var existingProduct = _items[existingProductIndex];
_items.removeAt(existingProductIndex);
notifyListeners();
final response = await http.delete(url);
if (response.statusCode >= 400) {
items.insert(existingProductIndex, existingProduct);
notifyListeners();
throw HttpException('Could Not Delete Product');
}
}
例如,执行是否会在 await http.delete... 中停止,直到它被解决,或者 if 语句块被认为包含在 .then 语句中等待 await 函数,而其余的则按应有的方式工作?
【问题讨论】:
-
这能回答你的问题吗? What is a Future and how do I use it?