Use Either with Future

1,453

It's all about <generics>.

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

You got to give the compiler all the proper information:

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

// or

.then((response) => right(response)) // a helper function which returns an Either
Share:
1,453
konstantin_doncov
Author by

konstantin_doncov

ML engineer and software developer

Updated on December 17, 2022

Comments

  • konstantin_doncov
    konstantin_doncov over 1 year

    I want to use dartz functional style and do something like this:

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

    But it seems that I can't do this:

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

    So, how can I use Either with Future this way?

  • Reiko Dev
    Reiko Dev about 3 years
    Helped a lot bro, ty