Skip to content

Instantly share code, notes, and snippets.

@minikin
Created September 30, 2020 13:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minikin/1550abe4f921188ebfa005db5f0604df to your computer and use it in GitHub Desktop.
Save minikin/1550abe4f921188ebfa005db5f0604df to your computer and use it in GitHub Desktop.
Result Type Dart
void main() {
final item = {'1': 'Apple'};
final operationSuccessed = Result.success(item);
final operationFailed = Result.error(Failure('Cannot catch data'));
print(operationSuccessed.result);
print(operationFailed.error);
}
class Result<Success, Error extends Failure> {
final Error error;
final Success result;
bool get isSuccess => error == null;
const Result.success(this.result) : error = null;
const Result.error(this.error) : result = null;
}
class Failure {
final String message;
const Failure(this.message);
@override
String toString() => message;
}
@minikin
Copy link
Author

minikin commented Sep 30, 2020

@anoop4real
Copy link

anoop4real commented Oct 3, 2020

@minkin nice one but I have question in real world scenario
Lets say I have a function like belwo

  Result<String, Error> getData() {
    
    return Result.success("Hello");
  }
  
  var result = getData();

result will be of a Result type ... how can I determine if it was an error or success without using the boolean you have

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment