[英]How to get Right of Future<Either<Error, List<dynamic>>> in Flutter?
I fetch data with code as below:我使用以下代码获取数据:
Future<Either<ResponseError, List<dynamic>>> _getResult(url) {
return RequestApi(url).fetchList();
}
I want to get List and use it to do something else, I knew how to use FutureBuilder to build Widget, but I want to try to use use Future, what can I do to get Right of Either?我想获取 List 并用它来做其他事情,我知道如何使用 FutureBuilder 来构建 Widget,但我想尝试使用 Future,我该怎么做才能获得 Right of Either? I tried
我试过了
I personally use a try catch and return left or right我个人使用 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.