- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 Dart 语言中的 Future 对象,并且一直在尝试使用它们。我想创建一个包含 future 对象的异步函数。
后者应该返回一个字符串,并且函数应该返回 future 返回的任何内容。
我知道我的文字不太好,但这里是代码和评论:
Future<String> getData(int number) async { // here I created my async function
// here is my future object assigned to a variable called thisFuture
Future<String> thisFuture = await Future.delayed(Duration(seconds: 1), () {
'this is a future string $number.';
});
// here, I am returning the future object
return thisFuture;
}
main(){
// inside my main function, I want to assign the above function to a variable without executing it
Future<String>captureFunc = getData; // I get an error
print(captureFunc(01)).then((e)=> print(e)); // here I want to access the string inside the future object.
// I am expecting to get: this is a future string 01.
}
我得到了异常(exception):
A value of type 'Future<String> Function(int)' can't be assigned to a variable of type 'Future<String>'.
This expression has a type of 'void' so its value can't be used.
The expression doesn't evaluate to a function, so it can't be invoked.
请帮助我了解函数内部的 future 、 future 以及如何从包含 future 类型对象的函数中获取值。
最佳答案
此行正在创建错误。
print(captureFunc(01)).then((e)=> print(e));
print 是一个带有 void 返回的简单内置函数,因此会产生错误。您应该首先调用 captureFunc 并使用然后回调来打印结果,例如 -
captureFunc(01).then((e)=> print(e));
此外,如果您想避免使用和链接 then 回调,那么您可以将 async-await 与 main 一起使用,因为 captureFunc return future 上面的代码可以这样清理 -
Future<String> getData(int number) async {
var thisFuture = await Future.delayed(Duration(seconds: 1), () {
return 'this is a future string $number.';
});
return thisFuture;
}
main() async {
var captureFunc = getData;
print(await captureFunc(01));
}
async-await 只是一种定义异步函数并使用其结果的声明性方式,它提供了语法糖来帮助您编写涉及 future 的干净代码。
关于object - Dart 和 flutter : Need help understanding how to capture futures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62546445/
我有一个字符串,我想多次使用它来输出 configure --help .所以我尝试在 configure.ac 中做这样的事情: AC_ARG_ENABLE([foobar],
我无法使其正确匹配。它只打印“[help]”,但我希望它匹配括号内的字符。 想要: [help] help 代码: Pattern p = Pattern.compile("\\[(\
我刚刚将vim74下载并安装到我的linux机器上。我只是为用户安装在本地。当我进入vim并执行:help时,我得到了错误。 我尝试添加: let $VIM='home/myuser/vim74' l
当我打开一个 Xcode 项目时,快速帮助功能工作得很好,但是当打开一个不在打开的项目中的文件(例如 abc.swift )时,文档不会出现。 下面是一个例子: 最佳答案 安装版本Command Li
我有一个 python click CLI。当我将 --help 传递给任何命令时,它会打印一条我喜欢的帮助消息。我发现现在很多用户都在输入 mycli help foo 而不是 mycli foo
我一直在使用 less 进行前端开发,但最近几天我遇到了这个错误。 我正在使用 PhpStorm 的观察器将 less 文件编译为 css 文件。但是当我编辑 less 文件时,编译器将这一行添加到
当我在 python 解释器中键入 help('string') 时,我得到了有关字符串类的信息。在那里,upper() 被表示为一个函数。然而,我只能将其称为 "hi".upper() 而不是 up
我希望 help 选项的作用与 --help 相同。 旁注:我创建了一个程序,其命令行行为与 svn 或 hg 相同。我设法用子解析器做到了这一点。但是我想让事情保持一致。这就是为什么我希望 help
我是 Maven 的新手。你知道我做错了什么吗(见下文)? mvn -e clean:help. + Error stacktraces are turned on. [INFO] Scannin
help format-list输出格式列表的帮助。 format-list | help输出帮助(get-help)。 最佳答案 该|或“管道”运算符将一个操作的输出重定向到另一个。因此,在这种情况
尝试从脚本检索帮助会出现以下错误: Get-Help : Cannot find Help for topic ".\Process-Test.ps1". At line:1 char:9 + get
我从常规的discord.py更改为重写版本,并且我曾经有一个自定义的!help命令。 现在,每当我发布 !help 时,它都会显示此内容以及编码的帮助消息: No Category: help S
我开发跨平台桌面应用程序,最近我发现了在小部件上使用帮助文本的乐趣。然后用户在标题栏上有一个问号按钮,单击该按钮后,他可以单击窗口中的任何小部件并查看有关它的帮助文本。非常好。 因为我认为所有程序都应
Xcode 4.5.2 Mountain Lion,Mac 应用 我严格遵守文档。帮助文件夹及其子文件夹已添加到项目/Resources 文件夹并显示为蓝色。 如有必要,文件夹引用由 xcode 添加
emulator documentation说: -audio-in Use the specified audio-input backend. 它还说 -help- Print
最近在学习argparse模块,代码下方出现Argument错误 import argparse import sys class ExecuteShell(object): def crea
我正在阅读有关 Maven 的教程,应该提到我对此一无所知。我已经成功地完成了第一章和第二章,这是关于它的设置的。但是在第 3 章“Maven Pom”中我遇到了问题。在最后一段中,它说在你想要的任何
我注意到 Windows 有这个问题,但我运行的是 Ubuntu 13.10(带有 Bash),所以我不知道出了什么问题。 尝试设置我的副本集,我运行以下命令: sudo bash # Accout
很抱歉,我在 python 程序中遇到了一个尴尬的问题。首先我是这里的新手,我是一名中国学生。请原谅我糟糕的英语。我会尽力把问题表达清楚。 错误代码在这里: def __find__(
好的,我试图环顾四周并弄清楚如何做到这一点,但无法弄清楚。这就是我想要做的。 # Run script normally .\myscript.ps1 "blah" "yo" 这些应该做同样的事情 G
我是一名优秀的程序员,十分优秀!