dart - Dart how to acces directly to a Map awaited data
问题描述
I have a function that return a Future<Map<String, dynamic>>
and I would like to return one of the map values instead of the whole map. For the moment I have to do it like this:
static Future<bool> fileClearCompleted() async {
var res = await rsApiCall('/rsFiles/FileDownloads');
return res['retval'];
}
rsApiCall
return a valid JSON object. So what I would like to find is a more elegant way to return the parameter res['retval']
like:
static Future<bool> fileClearCompleted() async => await rsApiCall('/rsFiles/FileDownloads')['retval'];
But this don't work, neither with cascade operator like: await rsApiCall('/rsFiles/FileDownloads')..['retval'];
解决方案
The correct syntax would be (see the added parenthesis):
static Future<bool> fileClearCompleted() async =>
(await rsApiCall('/rsFiles/FileDownloads'))['retval'];
Since we want to await
the rsApiCall
and then use the []
operator on that result.
推荐阅读
- java - Spring 中 Servlet 过滤器的内部工作原理
- docker - 为什么`chmod`会导致`docker build`耗尽磁盘空间
- vba - 在单独的文件中拆分 Access 数据库
- regex - 在 perl 中只打印匹配的字符
- prometheus - 如何划分两个 Prometheus 计数器
- javascript - 使用 WebRTC 时不立即显示视频
- xamarin.forms - FreshMVVM - 从不继承 FreshBasePageModel 的子 ContentView 打开页面的最佳方式
- windows - 如何为没有任何依赖关系的 Windows EXE 文件从头开始创建 Dockerfile?
- oracle - 没有通过递归过程得到正确的输出
- c# - 如何在我的 xamarin andriod 中将 TextView.Text 保存为文本文件