asynchronous - 将 Either 与 Future 一起使用

标签 asynchronous flutter dart functional-programming async-await

我想使用 dartz 函数式风格并执行如下操作:

Either<Failure, Response> result = await remoteDataSource.request() // Future<Response> request();
    .then((response) => Right(response))
    .catchError((failure) => Left(failure));

但我似乎做不到:

error: A value of type 'Right< dynamic, Response>' can't be assigned to a variable of type 'Either< Failure, Response>'.

那么,我怎样才能以这种方式将 EitherFuture 结合使用呢?

最佳答案

这都是关于<generics> .

.then((response) => Right(response)) // this is of type Right<dynamic,User>

您必须向编译器提供所有正确的信息:

.then((response) => Right<Failure, Response>(response))

// or

.then((response) => right(response)) // a helper function which returns an Either

关于asynchronous - 将 Either 与 Future 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59808248/

相关文章:

angular - 使用 async wait 推迟 block 的执行

flutter - 当特定条件为真时,如何在抖动中显示进度指示器?

google-chrome-extension - Dart Chrome 扩展 : Listen to chrome api events

dart - Flutter - 容器中的顶部对齐文本

flutter - 当flutter的提供程序中的多个类型相同时,如何获取值?

android - Flutter:圆形颜色选择器(包:flutter_colorpicker)

flutter - 如何在 Flutter 中正确绑定(bind)有状态小部件中的回调函数?

python - python中的异步I/O是什么?

java - 非阻塞 http 服务器,java nio,python tornado eventlet

c# - 从 WCF 服务调用异步方法