【发布时间】:2019-05-25 12:33:10
【问题描述】:
我在返回 Future 的类的末尾调用 notifyListeners(),就在 return 语句之前。 notifyListeners 发起的动作在调用 return 和处理后续语句之前没有完全完成,导致错误。似乎 notifyListeners 以某种方式异步完成。
我在另一个论坛上询问过这个问题,并被告知这是一个常见的颤振错误,尤其是在动画方面。在我看来,我更有可能在语法上做错了什么。有没有人遇到过这个“错误”?
我尝试同时使用 .then() 和 async / await。两者的结果相同。
Future<bool> updateProduct(Map<String, dynamic> updatedProduct)
...
return http
.put(
'https://chocolate-f3e81.firebaseio.com/products/${newProduct.serverID}.json',
body: json.encode(newProductMap))
.then<bool>((http.Response response) {
if (response.statusCode == 200 || response.statusCode == 201) {
_products[selectedProductIndex] = newProduct;
setIsLoading(false);
print('model selectedProductIndex ${selectedProductIndex}');
print('model selectedProductServerID ${_selectedProductServerID}');
notifyListeners();
return true;
我希望 notifyListeners() 启动的所有操作都在执行“return true”之前完成。这不会发生,而是执行返回操作,其他代码继续执行并领先于由 notifyListeners() 启动的代码。
【问题讨论】:
标签: flutter