Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get success or error data without folding the response while using fpdart in flutter?

I’m using fpdart for catching errors in my flutter app. I wanted to get error message or success message directly without folding the whole response. There is getRight() and getLeft() method on res object but it returns Option<Success Data Type> and Option<Error Data Type> respectively. How could i extract the data directly without folding?

Future<Either<String, int>> getData() async {
  try {
    int data = await Future.delayed(Duration(seconds: 3), () => 15);
    return right(data);
   } catch (err) {
    return left(err.toString());
  }
 }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I mostly create extension for this.

extension EitherExt on Either {
  ///! use only after checking the type
  get asLeft => (this as Left).value;
  get asRight => (this as Right).value;
}

And you can use like

final result = await getData(); 
///prefer checking `result.isRight()` on toplevel
final myInt = result.asRight;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading