I am making an application using flutter framework . During this I came across with the keywords in Dart "async" and "async*". Can anybody tell me what's the difference between them? Thanks in advance.
Sur*_*gch 158
async 给你一个 Futureasync*给你一个Stream。您将async关键字添加到执行一些可能需要很长时间的工作的函数。它返回包装在 a 中的结果Future。
Future<int> doSomeLongTask() async {
await Future.delayed(const Duration(seconds: 1));
return 42;
}
Run Code Online (Sandbox Code Playgroud)
您可以通过等待 Future 来获得该结果:
main() async {
int result = await doSomeLongTask();
print(result); // prints '42' after waiting 1 second
}
Run Code Online (Sandbox Code Playgroud)
您添加async*关键字以创建一个函数,该函数一次返回一堆未来值。结果被包装在一个流中。
Stream<int> countForOneMinute() async* {
for (int i = 1; i <= 60; i++) {
await Future.delayed(const Duration(seconds: 1));
yield i;
}
}
Run Code Online (Sandbox Code Playgroud)
技术术语是异步生成器功能。您使用yield返回值而不是return因为您没有离开函数。
您可以使用await for等待 Stream 发出的每个值。
main() async {
await for (int i in countForOneMinute()) {
print(i); // prints 1 to 60, one integer per second
}
}
Run Code Online (Sandbox Code Playgroud)
观看这些视频以了解更多信息,尤其是关于 Generators 的视频:
Rém*_*let 32
Marking a function as async or async* allows it to use async/await keyword to use a Future.
两者之间的区别在于,async*它将始终返回a Stream并提供一些语法糖以通过yield关键字发出值。
因此,我们可以执行以下操作:
Stream<int> foo() async* {
for (int i = 0; i < 42; i++) {
await Future.delayed(const Duration(seconds: 1));
yield i;
}
}
Run Code Online (Sandbox Code Playgroud)
该函数每秒发出一个值,每次都会递增
Sta*_*kin 21
这个答案包括简化且易于理解的示例
\nasync异步计算无法在启动时立即提供结果,因为程序可能需要等待外部响应,例如:
\n异步计算不会阻塞所有计算直到结果可用,而是立即返回一个 Future 对象,该对象最终将“完成”结果。
\n示例(这种类型的异步调用只能在不返回响应的情况下使用):
\nvoid main() async {\n // The next line awaits 5 seconds\n await Future.delayed(Duration(seconds: 5));\n // Pseudo API call that takes some time\n await fetchStocks();\n}\nRun Code Online (Sandbox Code Playgroud)\nFuture\n\nFuture 表示不会立即完成的计算。普通函数返回结果,而异步函数返回 Future,\n最终将包含结果。当结果准备好时,未来会告诉你。
\n
Stream)例子:
\nFuture<String> fetchUserOrder() =>\n // Imagine that this function is more complex and slow.\n Future.delayed(\n const Duration(seconds: 2),\n () => \'Large Latte\',\n );\n\nvoid main(List<String> arguments) async {\n var order = await fetchUserOrder(); \n // App awaits 2 seconds\n print(\'Your $order is ready\');\n}\nRun Code Online (Sandbox Code Playgroud)\nStream异步数据事件源。\n流提供了一种接收事件序列的方法。每个事件都是数据事件,也称为流的元素。
\nasync* (流)async*是一个返回 Stream 对象的异步生成器。专为创建流而设计。
使用流和 async* 的示例:
\n// Creating a new stream with async*\n// Each iteration, this stream yields a number\nStream<int> createNumberStream(int number) async* {\n for (int i = 1; i <= number; i++) {\n yield i;\n }\n}\n\nvoid main(List<String> arguments) {\n // Calling the stream generation\n var stream = createNumberStream(5);\n // Listening to Stream yielding each number\n stream.listen((s) => print(s));\n}\nRun Code Online (Sandbox Code Playgroud)\n结果:
\n1\n2\n3\n4\n5\nRun Code Online (Sandbox Code Playgroud)\n如果您已有流,则可以根据原始流\xe2\x80\x99s 事件将其转换为新流。
\n示例(与之前的代码相同但有所不同):
\nStream<int> createNumberStream(int number) async* {\n for (int i = 1; i <= number; i++) {\n yield i;\n }\n}\n\n// This part is taking a previous stream through itself and outputs updated values\n// This code multiplies each number from the stream\nStream<int> createNumberDoubling(Stream<int> chunk) async* {\n await for (final number in chunk) {\n yield number*2;\n }\n}\n\nvoid main(List<String> arguments) {\n // Here we are Transforming the first stream through createNumberDoubling stream generator\n var stream = createNumberDoubling(createNumberStream(5));\n stream.listen((s) => print(s));\n}\nRun Code Online (Sandbox Code Playgroud)\n结果:
\n2\n4\n6\n8\n10\nRun Code Online (Sandbox Code Playgroud)\n和async是async*近亲,它们甚至来自同一个库dart:async\nasync代表 aFuture和 一次性交换,而async*代表 a Stream,多个事件的流
| 归档时间: |
|
| 查看次数: |
2899 次 |
| 最近记录: |