dart - Dart 等待删除呢?
问题描述
等待删除“然后”吗?
为什么在下面使用 then 时会导致此错误:
// Error: [dart] The method 'then' isn't defined for the class 'String'. [undefined_method]
从这里修改代码
import 'dart:async';
Future<void> printDailyNewsDigest() async {
var newsDigest = await gatherNewsReports();
// Error: [dart] The method 'then' isn't defined for the class 'String'. [undefined_method]
newsDigest.then(print);
// print(newsDigest);
}
main() {
printDailyNewsDigest();
printWinningLotteryNumbers();
printWeatherForecast();
printBaseballScore();
}
printWinningLotteryNumbers() {
print('Winning lotto numbers: [23, 63, 87, 26, 2]');
}
printWeatherForecast() {
print("Tomorrow's forecast: 70F, sunny.");
}
printBaseballScore() {
print('Baseball score: Red Sox 10, Yankees 0');
}
const news = '<gathered news goes here>';
const oneSecond = Duration(seconds: 1);
// Imagine that this function is more complex and slow. :)
Future<String> gatherNewsReports() =>
Future.delayed(oneSecond, () => news);
解决方案
await
不会“删除” then
,但它允许以更方便的语法编写异步代码,例如 withthen
和一种 replaces then
。然后由编译器
await
重写回。then
await
推迟执行以下代码,直到等待的异步操作的结果完成并返回结果值。
结果值Future
不再是 a,因此then()
不可用。
推荐阅读
- python - 如何找到csv文件中列的平均值?
- flutter - 屏幕底部的发送消息文本字段
- javascript - 为什么我的查找奇数的 JavaScript 程序不起作用?
- python - Python小浮点数 - 算术精度
- init.d - 如何定义 log_daemon_msg、log_end_msg、log_progress_msg 的存储位置?
- c# - 绑定到 ResourceDictionary 中的属性
- database - Redis 关闭自动备份/转储
- python - 下拉菜单后面的网页抓取页面
- mysql - Maria JDBC 驱动程序未找到 CommunicationsError
- numpy - PIL 到 numpy 和 PIL 到 tensor 是不同的