{"id":13549784,"url":"https://github.com/cbyad/either_option","last_synced_at":"2025-04-09T16:07:41.911Z","repository":{"id":47449178,"uuid":"194839702","full_name":"cbyad/either_option","owner":"cbyad","description":"A small typed and safe library for error handling with functionnal programming concept in Dart and flutter project","archived":false,"fork":false,"pushed_at":"2022-10-31T09:36:48.000Z","size":30,"stargazers_count":34,"open_issues_count":1,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T16:07:31.005Z","etag":null,"topics":["dart","either","either-monad","flutter","functional-programming","maybe","option","option-monad","result"],"latest_commit_sha":null,"homepage":"","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cbyad.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-02T10:12:21.000Z","updated_at":"2023-02-28T14:39:45.000Z","dependencies_parsed_at":"2022-08-23T11:10:15.834Z","dependency_job_id":null,"html_url":"https://github.com/cbyad/either_option","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cbyad%2Feither_option","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cbyad%2Feither_option/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cbyad%2Feither_option/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cbyad%2Feither_option/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cbyad","download_url":"https://codeload.github.com/cbyad/either_option/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248065287,"owners_count":21041871,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dart","either","either-monad","flutter","functional-programming","maybe","option","option-monad","result"],"created_at":"2024-08-01T12:01:25.518Z","updated_at":"2025-04-09T16:07:41.889Z","avatar_url":"https://github.com/cbyad.png","language":"Dart","funding_links":[],"categories":["Dart"],"sub_categories":[],"readme":"# either_option\n\n__either_option__ is a simple library typed for easy and safe error handling with functional programming style in Dart.\nIt aims to allow flutter/dart developpers to use the 2 most popular patterns and abstractions : \n`Either` and `Option`, mainly used in FP language like Scala, Haskell, OCaml,...\n\n# Installation\n\nPrerelease versions that support null safety\n\nPackage link on pub [either_option](https://pub.dev/packages/either_option)\nIn your pubspec.yaml dependencies add  \n\n       either_option: ^2.0.0\n\n# Overview\n## Either\n`Either` Represents a value of one of two possible types.\nBy convention we put missing or error value in an instance of `Left` and expected success value in an instance of `Right`.\n\nFor example, we fetch an url from a repository [repository](example/lib/repository_example.dart) to get an *User* details and use `Either\u003cServerError,User\u003e` :\n\n```dart\n  Future\u003cEither\u003cServerError, User\u003e\u003e getUser(int id) async {\n    final baseUrl = \"https://fakerestapi.azurewebsites.net/api/Users/$id\";\n\n    final res = await http.get(baseUrl);\n\n    if (res.statusCode == 200) {\n      dynamic body = json.decode(res.body);\n      User user = User.fromJson(body);\n      return Right(user);\n    }\n    if (res.statusCode == 404)\n      return Left(ServerError(\"This user doesn't exist\"));\n\n    return Left(ServerError(\"Unknown server error\"));\n  }\n```\nSo now to consume result we can use for example `fold` method and say what to do with value :\n\n```dart\nmain() async {\n  final Repository repository = Repository();\n\n  final Either\u003cServerError, User\u003e res = await repository.getUser(3);\n  final defaultUser = User(id: 0, username: \"ko\", password: \"ko\");\n  final userName = res.fold((_) =\u003e defaultUser.username, (user) =\u003e user.username);\n  print(userName); // \"User 3\"\n  \n//if res was a Left, print(userName) would give \"ko\"\n}\n```\n## Option\n`Option` Represents a value of one of two possible types.\nBy convention we consider missing value as an instance of `None` and expected success value in an instance of `Some`.\n\n```dart\n  Future\u003cOption\u003cUser\u003e\u003e getUserOpt(int id) async {\n    final res = await http.get(baseUrl);\n    return Option.cond(\n        res.statusCode == 200, User.fromJson(json.decode(res.body)));\n  }\n  // -----------------------------------------\n  main() async {\n  final Repository repository = Repository();\n\n  final Option\u003cUser\u003e res = await repository.getUserOpt(3);\n  final defaultUser = User(id: 0, username: \"ko\", password: \"ko\");\n  final userName = res.getOrElse(defaultUser).username;\n  print(userName); // \"User 3\"\n\n  //if res was a None, print(userName) would give \"ko\"\n}\n```\n\n## Ressources \u0026 explanation\n[Medium article](https://medium.com/@cb.yannick/a-small-monad-library-for-dart-flutter-project-e49b71205bd0?source=friends_link\u0026sk=21c7527322b281aa6e161bad27086952)\n# Features\nFunctions available :\n\n|              | Option    | Either|\n| ------------ |:---------:| -----:|\n| fold         |   :+1:    |    :+1:    |\n| map          |     :+1:  |   :+1:     |\n| flatMap      |     :+1:  |    :+1:    |\n| getOrElse    |     :+1:  |       |\n| orElse       |      :+1:      |       |\n| toLeft       |     :+1:       |       |\n| toRight      |     :+1:       |       |\n| toEither     |     :+1:       |       |\n| Option.empty |     :+1:       |       |\n| Option.of    |     :+1:       |       |\n| filter       |     :+1:       |       |\n| exists       |     :+1:       |       |\n| contains     |     :+1:       |       |\n| swap         |                |   :+1:|\n| cond         |       :+1:      |   :+1:|\n\n\n\n# Example\n* [Either \u0026 Option](example/lib/either_option_example.dart)\n* [Repository](example/lib/repository_example.dart)\n\n# Tests\n* [Either](test/either_test.dart)\n* [Option](test/option_test.dart)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcbyad%2Feither_option","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcbyad%2Feither_option","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcbyad%2Feither_option/lists"}