flutter - 从未来中提取价值
问题描述
我想从 api 调用中提取响应的值。
import 'package:http/http.dart' as http;
refreshToken() {
Future<http.Response> _response = _httpClient.callApi(
endPoint: refreshTokenEndpoint, variables: refreshTokenVariables);
http.Response _tokenResponse;
_response.then((http.Response value) => _tokenResponse = value);
print(_tokenResponse.body);
}
但是当我尝试访问正文时,我得到一个错误 body was called on null 。
解决方案
你需要等待,_tokenResponse
因为来自未来。
refreshTokenSolution() async {
Future<bool> _response = Future.delayed(Duration(seconds: 2), () => true);
bool _tokenResponse = await _response; // Wait this response and after go next line
print(_tokenResponse); //TRUE
}
推荐阅读
- r - R-在合并中返回不匹配的产品
- r - 使用 library('tidyverse') 而不是子包有什么好处?
- sql-server - SQL Server 使用触发器插入多行
- php - 如何确定 strpos 的优先级以找到最准确的结果
- yocto - Yocto 构建成功,但警告缺少 RDEPENDS
- python - python中的链表和递归
- node.js - 在 Amazon Alexa 中出现错误
- user-interface - 在 Ubuntu 14.04 的 GNU Octave 命令窗口和编辑器中只有一半的行可见?
- python-3.x - 检查两个输入是否在 2 个不同的列表中具有相同的索引
- csv - 我们如何在 AppDrag 上为我的产品导入 CSV?