{"id":15554955,"url":"https://github.com/flutterando/result_dart","last_synced_at":"2025-04-09T13:09:39.134Z","repository":{"id":64867497,"uuid":"579047999","full_name":"Flutterando/result_dart","owner":"Flutterando","description":"Result for dart. It is an implementation based on Kotlin Result and Swift Result.","archived":false,"fork":false,"pushed_at":"2024-12-11T20:44:04.000Z","size":153,"stargazers_count":43,"open_issues_count":3,"forks_count":13,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-09T13:09:33.795Z","etag":null,"topics":["dart"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/result_dart","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/Flutterando.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-16T14:26:41.000Z","updated_at":"2025-03-28T05:30:37.000Z","dependencies_parsed_at":"2024-12-10T23:33:01.260Z","dependency_job_id":null,"html_url":"https://github.com/Flutterando/result_dart","commit_stats":{"total_commits":37,"total_committers":4,"mean_commits":9.25,"dds":"0.18918918918918914","last_synced_commit":"b4316d63c9cf681fd2c0bf37334043224062af5b"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flutterando%2Fresult_dart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flutterando%2Fresult_dart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flutterando%2Fresult_dart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flutterando%2Fresult_dart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Flutterando","download_url":"https://codeload.github.com/Flutterando/result_dart/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248045245,"owners_count":21038554,"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"],"created_at":"2024-10-02T15:05:20.739Z","updated_at":"2025-04-09T13:09:39.114Z","avatar_url":"https://github.com/Flutterando.png","language":"Dart","readme":"\u003ca name=\"readme-top\"\u003e\u003c/a\u003e\n\n\n\u003ch1 align=\"center\"\u003eRESULT_DART\u003c/h1\u003e\n\n\u003c!-- PROJECT LOGO --\u003e\n\u003cbr /\u003e\n\u003c!-- \u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://github.com/Flutterando/README-Template/\"\u003e\n    \u003cimg src=\"https://raw.githubusercontent.com/Flutterando/README-Template/master/readme_assets/logo.png\" alt=\"Logo\" width=\"180\"\u003e\n  \u003c/a\u003e --\u003e\n\n  \u003cp align=\"center\"\u003e\n    This package aims to create an implemetation of \u003cb\u003eKotlin's and Swift's Result class and own operators\u003c/b\u003e.\n    Inspired by `multiple_result` package, the `dartz` package and the `fpdart` package.  \n    \u003cbr /\u003e\n    \u003c!-- Put the link for the documentation here --\u003e\n    \u003ca href=\"https://pub.dev/documentation/result_dart/latest/\"\u003e\u003cstrong\u003eExplore the docs »\u003c/strong\u003e\u003c/a\u003e\n    \u003cbr /\u003e\n    \u003cbr /\u003e\n    \u003c!-- Disable unused links with with comments --\u003e\n    \u003ca href=\"https://github.com/Flutterando/result_dart/issues\"\u003eReport Bug\u003c/a\u003e\n    ·\n    \u003ca href=\"https://github.com/Flutterando/result_dart/pulls\"\u003eRequest Feature\u003c/a\u003e\n  \u003c/p\u003e\n\n\u003cbr\u003e\n\n\u003cdiv align='center'\u003e\n\n[![Version](https://img.shields.io/github/v/release/Flutterando/result_dart?style=plastic)](https://pub.dev/packages/result_dart)\n[![Pub Points](https://img.shields.io/pub/points/result_dart?label=pub%20points\u0026style=plastic)](https://pub.dev/packages/result_dart/score)\n[![Flutterando Analysis](https://img.shields.io/badge/style-flutterando__analysis-blueviolet?style=plastic)](https://pub.dev/packages/flutterando_analysis/)\n\n[![Pub Publisher](https://img.shields.io/pub/publisher/result_dart?style=plastic)](https://pub.dev/publishers/flutterando.com.br/packages)\n\n\n\u003c/div\u003e\n\n\u003cbr\u003e\n\n---\n\u003c!-- TABLE OF CONTENTS --\u003e\n\u003c!-- Linked to every ## title below --\u003e\n\u003cdetails\u003e\n  \u003csummary\u003eTable of Contents\u003c/summary\u003e\n  \u003col\u003e\n    \u003cli\u003e\u003ca href=\"#about-the-project\"\u003eAbout The Project\u003c/a\u003e\u003c/li\u003e\n m    \u003cli\u003e\u003ca href=\"#getting-started\"\u003eGetting Started\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"#how-to-use\"\u003eHow to Use\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"#features\"\u003eFeatures\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"#contributing\"\u003eContributing\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"#contact\"\u003eContact\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"#acknowledgements\"\u003eAcknowledgements\u003c/a\u003e\u003c/li\u003e\n  \u003c/ol\u003e\n\u003c/details\u003e\n\n---\n\n\u003cbr\u003e\n\n\u003c!-- ABOUT THE PROJECT --\u003e\n## About The Project\n\n\n\u003c!-- PROJECT EXAMPLE (IMAGE) --\u003e\n\n\u003cbr\u003e\n\u003c!-- \u003cCenter\u003e\n\u003cimg src=\"https://raw.githubusercontent.com/Flutterando/README-Template/master/readme_assets/project-image.png\" alt=\"Project Screenshot\" width=\"400\"\u003e\n\u003c/Center\u003e --\u003e\n\n\u003cbr\u003e\n\n\u003c!-- PROJECT DESCRIPTION --\u003e\n\nOverruns are common in design, and modern architectures always designate a place to handle failures.\nThis means dramatically decreasing try/catch usage and keeping treatments in one place.\nBut the other layers of code need to know about the two main values `[Success, Failure]`. The solution lies in the\n`Result` class pattern implemented in `Kotlin` and `Swift` and now also in `Dart` via this package(`result_dart`).\n\n\u003ci\u003eThis project is distributed under the MIT License. See `LICENSE` for more information.\n\u003c/i\u003e\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#readme-top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\n## Migrate 1.1.1 to 2.0.0\n\nThis version aims to reduce the `Result` boilerplate by making the `Failure` type Exception by default. This will free the Result from having to type `Failure`, making the declaration smaller.\n\n```dart\n// Old\nResult\u003cint, Exception\u003e myResult = Success(42);\n\n// NEW\nResult\u003cint\u003e myResult = Success(42);\n\n```\n\nif there is a need to type it, use `ResultDart\u003cSuccess, Failure\u003e`:\n\n```dart\n// Old\nResult\u003cint, String\u003e myResult = Success(42);\n\n// NEW\nResultDart\u003cint, String\u003e myResult = Success(42);\n\n```\n\n\n\u003c!-- GETTING STARTED --\u003e\n## Getting Started\n\n\u003c!---- The description provided below was aimed to show how to install a pub.dev package, change it as you see fit for your project ----\u003e\nTo get `result_dart` working in your project follow either of the instructions below:\n\na) Add `result_dart` as a dependency in your Pubspec.yaml:\n ```yaml\n   dependencies:\n     result_dart: x.x.x\n``` \n\nb) Use Dart Pub:\n```\n  dart pub add result_dart\n```\n\n\u003cbr\u003e\n\n\n## How to Use\n\n\nIn the return of a function that you want to receive an answer as Sucess or Failure, set it to return a Result type;\n\n```dart\nResult getSomethingPretty();\n```\n\nthen add the Success and the Failure types.\n\n```dart\nResult\u003cString\u003e getSomethingPretty() {\n\n}\n\n```\n\nIn the return of the above function, you just need to use:\n```dart\n// Using Normal instance\nreturn Success('Something Pretty');\n\n// import 'package:result_dart/functions.dart'\nreturn successOf('Something Pretty');\n\n// Using extensions\nreturn 'Something Pretty'.toSuccess();\n```\n\nor\n\n```dart\n// Using Normal instance\nreturn Failure(Exception('something ugly happened...'));\n\n// import 'package:result_dart/functions.dart'\nreturn failureOf('Something Pretty');\n\n// Using extensions\nreturn 'something ugly happened...'.toFailure();\n```\n\nThe function should look something like this:\n\n```dart\n\nResult\u003cString\u003e getSomethingPretty() {\n    if(isOk) {\n        return Success('OK!');\n    } else {\n        return Failure(Exception('Not Ok!'));\n    }\n}\n\n```\nor when using extensions, like this:\n\n```dart\n\nResult\u003cString\u003e getSomethingPretty() {\n    if(isOk) {\n        return 'OK!'.toSuccess();\n    } else {\n        return Exception('Not Ok!').toFailure();\n    }\n}\n\n```\n\n\u003e IMPORTANT NOTE: The `toSuccess()` and `toFailure()` methods cannot be used on a `Result` object or a `Future`. If you try, will be throw a Assertion exception.\n\n\u003cbr\u003e\n\n#### Handling the Result with `fold`:\n\nReturns the result of onSuccess for the encapsulated value\nif this instance represents `Success` or the result of onError function\nfor the encapsulated value if it is `Failure`.\n\n```dart\nvoid main() {\n    final result = getSomethingPretty();\n     final String message = result.fold(\n        (success) {\n          // handle the success here\n          return \"success\";\n        },\n         (failure) {\n          // handle the failure here\n          return \"failure\";\n        },\n    );\n\n}\n```\n\n#### Handling the Result with `getOrThrow`\n\nReturns the success value as a throwing expression.\n\n```dart\nvoid main() {\n    final result = getSomethingPretty();\n\n    try {\n     final value = result.getOrThrow();\n    } on Exception catch(e){\n      // e\n    }\n}\n\n```\n\n#### Handling the Result with `getOrNull`\n\nReturns the value of [Success] or null.\n\n```dart\nvoid main() {\n    final result = getSomethingPretty();\n    result.getOrNull();\n}\n\n```\n\n#### Handling the Result with `getOrElse`\n\nReturns the encapsulated value if this instance represents `Success`\nor the result of `onFailure` function for\nthe encapsulated a `Failure` value.\n\n```dart\nvoid main() {\n    final result = getSomethingPretty();\n    result.getOrElse((failure) =\u003e 'OK');\n}\n\n```\n\n#### Handling the Result with `getOrDefault`\n\nReturns the encapsulated value if this instance represents\n`Success` or the `defaultValue` if it is `Failure`.\n\n```dart\nvoid main() {\n    final result = getSomethingPretty();\n    result.getOrDefault('OK');\n}\n\n```\n\n\n#### Handling the Result with `exceptionOrNull`\n\nReturns the value of [Failure] or null.\n\n```dart\nvoid main() {\n    final result = getSomethingPretty();\n    result.exceptionOrNull();\n}\n```\n\n### Transforming a Result\n\n#### Mapping success value with `map`\n\nReturns a new `Result`, mapping any `Success` value\nusing the given transformation.\n\n```dart\nvoid main() {\n    final result = getResult()\n        .map((e) =\u003e MyObject.fromMap(e));\n\n    result.getOrNull(); //Instance of 'MyObject' \n}\n```\n\n#### Mapping failure value with `mapError`\n\nReturns a new `Result`, mapping any `Error` value\nusing the given transformation.\n\n```dart\nvoid main() {\n    final result = getResult()\n        .mapError((e) =\u003e MyException(e));\n\n    result.exceptionOrNull(); //Instance of 'MyException'\n\n}\n```\n\n#### Chain others [Result] by any `Success` value with `flatMap`\n\n\nReturns a new `Result`, mapping any `Success` value\nusing the given transformation and unwrapping the produced `Result`.\n\n```dart\n\nResult\u003cString, MyException\u003e checkIsEven(String input){\n    if(input % 2 == 0){\n        return Success(input);\n    } else {\n        return Failure(MyException('isn`t even!'));\n    }\n}\n\nvoid main() {\n    final result = getNumberResult()\n        .flatMap((s) =\u003e checkIsEven(s));\n}\n```\n#### Chain others [Result] by `Failure` value with `flatMapError`\n\n\nReturns a new `Result`, mapping any `Error` value\nusing the given transformation and unwrapping the produced `Result`.\n\n```dart\n\nvoid main() {\n    final result = getNumberResult()\n        .flatMapError((e) =\u003e checkError(e));\n}\n```\n\n#### Resolve [Result] by `Failure` value with `recover`\n\nReturns the encapsulated `Result` of the given transform function\napplied to the encapsulated a `Failure` or the original\nencapsulated value if it is success.\n\n```dart\n\nvoid main() {\n    final result = getNumberResult()\n        .recover((f) =\u003e Success('Resolved!'));\n}\n```\n\n#### Add a pure `Success` value with `pure`\n\nChange the [Success] value.\n\n```dart\nvoid main() {\n    final result = getSomethingPretty().pure(10);\n\n    String? mySuccessResult;\n    if (result.isSuccess()) {\n      mySuccessResult = result.getOrNull(); // 10\n    }\n}\n```\n\n#### Add a pure `Failure` value with `pureError`\n\nChange the [Failure] value.\n\n```dart\nvoid main() {\n    final result = getSomethingPretty().pureError(10);\n    if (result.isFailure()) {\n       result.exceptionOrNull(); // 10\n    }\n}\n```\n#### Swap a `Result` with `swap`\n\nSwap the values contained inside the [Success] and [Failure]\nof this [Result].\n\n```dart\nvoid main() {\n    Result\u003cString, int\u003e result =...;\n    Result\u003cint, String\u003e newResult = result.swap();\n}\n```\n\n### Unit Type\n\nSome results do not need a specific return. Use the Unit type to signal an **empty** return.\n\n```dart\n    Result\u003cUnit\u003e\n```\n\n### Help with functions that return their parameter:\n\nNOTE: use import 'package:result_dart/functions.dart'\n\n\nSometimes it is necessary to return the parameter of the function as in this example:\n\n```dart\nfinal result = Success(0);\n\nString value = result.when((s) =\u003e '$s', (e) =\u003e e);\nprint(string) // \"0\";\n```\n\nWe can use the `identity` function or its acronym `id` to facilitate the declaration of this type of function that returns its own parameter and does nothing else:\n\n```dart\nfinal result = Success(0);\n\n// changed `(e) =\u003e e` by `id`\nString value = result.when((s) =\u003e '$s', id);\nprint(string) // \"0\";\n```\n\n### Use **AsyncResult** type:\n\n`AsyncResult\u003cS, E\u003e` represents an asynchronous computation.\nUse this component when working with asynchronous **Result**.\n\n**AsyncResult** has some of the operators of the **Result** object to perform data transformations (**Success** or **Failure**) before executing the Future.\n\nAll **Result** operators is available in **AsyncResult**\n\n`AsyncResult\u003cS, E\u003e` is a **typedef** of `Future\u003cResult\u003cS, E\u003e\u003e`.\n\n```dart\n\nAsyncResult\u003cString\u003e fetchProducts() async {\n    try {\n      final response = await dio.get('/products');\n      final products = ProductModel.fromList(response.data);\n      return Success(products);\n    } on DioError catch (e) {\n      return Failure(ProductException(e.message));\n    }\n}\n\n...\n\nfinal state = await fetch()\n    .map((products) =\u003e LoadedState(products))\n    .mapLeft((failure) =\u003e ErrorState(failure))\n\n```\n\u003cbr\u003e\n\n\u003c!-- _For more examples, please refer to the_ [Documentation]()  --\u003e\n\n\u003c!---- You can use the emoji 🚧 to indicate Work In Progress sections ----\u003e\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#readme-top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\u003c!-- FEATURES --\u003e\n\n\u003c!---- Marcar com ✅ o que foi feito\ne 🚧 para o que está sendo trabalhado ----\u003e\n## Features\n\n- ✅ Result implementation.\n- ✅ Result`s operators(map, flatMap, mapError, flatMapError, swap, when, fold, getOrNull, exceptionOrNull, isSuccess, isError).\n- ✅ AsyncResult implementation.\n- ✅ AsyncResult`s operators(map, flatMap, mapError, flatMapError, swap, when, fold, getOrNull, exceptionOrNull, isSuccess, isError).\n- ✅ Auxiliar functions (id, identity, success, failure).\n- ✅ Unit type.\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#readme-top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\n\n\u003c!-- CONTRIBUTING --\u003e\n## Contributing\n\nContributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.\n\nIf you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the appropriate tag. \nDon't forget to give the project a star! Thanks again!\n\n1. Fork the Project\n2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)\n3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)\n4. Push to the Branch (`git push origin feature/AmazingFeature`)\n5. Open a Pull Request\n\nRemember to include a tag, and to follow [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) and [Semantic Versioning](https://semver.org/) when uploading your commit and/or creating the issue. \n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#readme-top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\u003c!-- CONTACT --\u003e\n\n\u003c!---- Those are the current Flutterando contacts as of 25 August 2022 ---\u003e\n## Contact\n\nFlutterando Community\n- [Discord](https://discord.flutterando.com.br/)\n- [Telegram](https://t.me/flutterando)\n- [Website](https://www.flutterando.com.br)\n- [Youtube Channel](https://www.youtube.com.br/flutterando)\n- [Other useful links](https://linktr.ee/flutterando)\n\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#readme-top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\u003c!-- ACKNOWLEDGEMENTS --\u003e\n## Acknowledgements \n\n\nThank you to all the people who contributed to this project, whithout you this project would not be here today.\n\n\u003cbr\u003e\n\n\u003c!---- Change the link below to the contributors page of your project and change the repo= in the img src to properly point to your repository --\u003e\n\n\u003ca href=\"https://github.com/flutterando/result_dart/graphs/contributors\"\u003e\n  \u003cimg src=\"https://contrib.rocks/image?repo=flutterando/result_dart\" /\u003e\n\u003c/a\u003e\n\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#readme-top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\u003c!-- MANTAINED BY --\u003e\n## Maintaned by\n\n\u003cbr\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.flutterando.com.br\"\u003e\n    \u003cimg width=\"110px\" src=\"https://raw.githubusercontent.com/Flutterando/README-Template/master/readme_assets/logo-flutterando.png\"\u003e\n  \u003c/a\u003e\n  \u003cp align=\"center\"\u003e\n    Built and maintained by \u003ca href=\"https://www.flutterando.com.br\"\u003eFlutterando\u003c/a\u003e.\n  \u003c/p\u003e\n\u003c/p\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflutterando%2Fresult_dart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflutterando%2Fresult_dart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflutterando%2Fresult_dart/lists"}