>> 在颤振中?,flutter,future,either"/>

首页 > 解决方案 > 如何获得未来的权利>> 在颤振中?

问题描述

我使用以下代码获取数据:

Future<Either<ResponseError, List<dynamic>>> _getResult(url) {
  return  RequestApi(url).fetchList();
}

我想获取 List 并用它来做其他事情,我知道如何使用 FutureBuilder 来构建 Widget,但我想尝试使用 Future,我该怎么做才能获得 Right of Either?我试过了

标签: flutterfutureeither

解决方案


我个人使用 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);
    }
}

推荐阅读