【问题标题】:How to get Right of Future<Either<Error, List<dynamic>>> in Flutter?如何在 Flutter 中获得 Future<Either<Error, List<dynamic>>> 的权利?
【发布时间】:2020-12-30 08:41:10
【问题描述】:

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

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

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

【问题讨论】:

    标签: flutter future either


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2018-12-01
      • 2021-10-05
      • 2021-10-20
      • 1970-01-01
      • 2019-09-08
      • 1970-01-01
      • 2021-10-22
      • 2021-01-18
      • 1970-01-01
      相关资源
      最近更新 更多