我想使用 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>'.
那么,我怎样才能以这种方式将 Either
与 Future
结合使用呢?
最佳答案
这都是关于<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/