flutter - 如何获得未来的权利>> 在颤振中?
问题描述
我使用以下代码获取数据:
Future<Either<ResponseError, List<dynamic>>> _getResult(url) {
return RequestApi(url).fetchList();
}
我想获取 List 并用它来做其他事情,我知道如何使用 FutureBuilder 来构建 Widget,但我想尝试使用 Future,我该怎么做才能获得 Right of Either?我试过了
解决方案
我个人使用 try catch 并返回左或右
Future<Either<List<dynamic>>, Exception> _getResult(url) async {
try {
final response = await http.post(url);
if (response == null) throw ResponseNullException();
return Left(response.asList());
} catch (e, s) {
return Right(e);
}
}
推荐阅读
- python - Holoviews 热图 X 轴格式
- asp.net-mvc - .net 4.7 中的依赖注入?
- javascript - 如何使用 vanilla JavaScript 更改此 SVG 精灵的 href?
- python - 如何将 tkinter 按钮与标签和输入框保持在同一行
- snakemake - Snakemake 规则仅针对一个文件运行
- python - 在 Python 中读取 n qty 行分隔的 txt 文件
- c - 使用 lex 解析时处理文件名中的空格
- java - Ubuntu 18.04.1 LTS 桌面:如何以编程方式获取任何桌面应用程序的窗口位置(X、Y)
- android-studio - Android Studio:如何在多个按钮上使用 ViewPagerAdapter 来加载不同的图像?
- ruby-on-rails - 帮助器中未定义的 Number_to_currency (是视图或控制器的帮助器吗?)