How to extract Left or Right easily from Either type in Dart (Dartz)

Ok here the solutions of my problems:

To extract/retrieve the data

final Either<ServerException, TokenModel> result = await repository.getToken(...);
result.fold(
 (exception) => DoWhatYouWantWithException, 
 (tokenModel) => DoWhatYouWantWithModel
);

//Other way to 'extract' the data
if (result.isRight()) {
  final TokenModel tokenModel = result.getOrElse(null);
}

To test the exception

//You can extract it from below, or test it directly with the type
expect(() => result, throwsA(isInstanceOf<ServerException>()));

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)