dart - 那么Dart等待移除吗?

标签 dart

是否等待删除“然后”?

为什么在以下情况下使用then会导致此错误:

// Error: [dart] The method 'then' isn't defined for the class 'String'. [undefined_method]

here的修改后的代码
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,但它允许像then一样以更方便的语法编写异步代码,并且可以替换then
然后,编译器将await重新写回then
await延迟以下代码的执行,直到完成等待的异步操作的结果并返回结果值。
结果值不再是Future,因此then()不可用。

关于dart - 那么Dart等待移除吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53498805/

相关文章:

android - 等待阻塞 GC ProfileSaver

dart - 如何在 Dart 构造函数中设置 final/const 属性

android - Flutter:在 ListView Builder 中使用英雄动画

flutter - Flutter Infinite滚动:如何触发每个滚动一个

flutter - _断言错误('package:firebase_auth/src/firebase_auth.dart' : Failed assertion: line 95 pos 12: 'email != null' : is not true.)

dart - 编写只引用一个元素的单例组件

dart - 更改下拉值时文本字段中的值重置

flutter - 如何制作一堆可检测手势的自定义绘制形状

php - 如何在 dart 的 Future 函数中从 JSON 创建一个类对象?

dart - 在 polymer.dart 中绑定(bind)表格