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

后端 未结 3 567
抹茶落季
抹茶落季 2021-02-04 09:53

I am looking to extract a value easily from a method that return a type Either.

I am doing some tests but unable to test easily th

3条回答
  •  醉话见心
    2021-02-04 10:39

    Ok here the solutions of my problems:

    To extract/retrieve the data

    final Either 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()));
    

提交回复
热议问题