我有以下几行,但不起作用。它说:预期;等待之后。
import "package:http/http.dart" as http;
http.Response rslt = await http.post(/*...*/);
由于 http.post
根据定义返回一个 Future(Response),难道 await
不能解决这个问题吗?我以为会。
https://www.dartdocs.org/documentation/http/0.11.3%2B3/http/http-library.html
从某种意义上说,我一直认为 await 会解开 Future 对象并将其分配给任何...在本例中为 Response 变量。
最佳答案
我很确定包含此代码的方法/函数缺少 async
修饰符。
Future someFunc() async {
import "package:http/http.dart" as http;
http.Response rslt = await http.post(/*...*/);
}
如果函数体没有 async
修饰符,则 async
是一个有效的标识符,因为 async
不是关键字。
关于dart - 为什么我不能通过等待将变量分配给异步调用的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35160527/