{"id":24562868,"url":"https://github.com/kmartins/result_kt","last_synced_at":"2025-04-19T17:43:07.342Z","repository":{"id":40001593,"uuid":"448116236","full_name":"kmartins/result_kt","owner":"kmartins","description":"Encapsulate a value as a success or an error as a failure using Result and execute a function using runCatching that returns a Result, both similar to those found in Kotlin.","archived":false,"fork":false,"pushed_at":"2024-12-11T17:26:28.000Z","size":24,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T11:01:36.576Z","etag":null,"topics":["dart","either","either-dart","flutter","result","result-type"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/result_kt","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/kmartins.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-01-14T21:48:54.000Z","updated_at":"2024-12-11T17:26:31.000Z","dependencies_parsed_at":"2024-12-19T14:01:03.809Z","dependency_job_id":"d2ca4091-9673-46c6-87ec-b0909408a4f4","html_url":"https://github.com/kmartins/result_kt","commit_stats":{"total_commits":17,"total_committers":1,"mean_commits":17.0,"dds":0.0,"last_synced_commit":"598168964ad8f33f945b14f9ed2e4a1f29605a79"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmartins%2Fresult_kt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmartins%2Fresult_kt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmartins%2Fresult_kt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmartins%2Fresult_kt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kmartins","download_url":"https://codeload.github.com/kmartins/result_kt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249751653,"owners_count":21320352,"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-dart","flutter","result","result-type"],"created_at":"2025-01-23T09:18:31.156Z","updated_at":"2025-04-19T17:43:07.315Z","avatar_url":"https://github.com/kmartins.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Result Kt\n\n[![pub][package_badge]][package_link]\n[![License: MIT][license_badge]][license_link]\n[![result_kt][workflow_badge]][workflow_link]\n[![codecov][codecov_badge]][codecov_link]\n\nA Dart package with the type `Result` and the `runCatching` function that are similar to those found in [Kotlin][kotlin_result].\n\nYou can use it with the `Stream` too 🙌\n\n## Why?\n\nI needed something as is done in `Kotlin` and did not want to add other functions that not are used in my projects.\n\nThere is another package similar, [result][resultk], however, it was discontinued.\n\n## Usage\n\nAdd it in your `pubspec.yaml`:\n\n```yaml\ndependencies:\n  result_kt:\n```\n\nImport it where you want to use it e.g, in your main file.\n\n```dart\nimport 'package:result_kt/result_kt.dart';\n```\n\n## Overview\n\n### Result\n\nYou can encapsulate a successful outcome\nwith a value or a failure with an  `error` and stack trace\nIt is similar to an `Either` that you see in other packages as [dartz][dartz], [fpdart][fpdart]...\n\n```dart\nResult.success(0)\n    .onSuccess(\n        action: print,\n    )\n    .onFailure(\n    action: (failure) =\u003e print(\n            failure.error.toString(),\n        ),\n    );\n\nResult\u003cint\u003e.failure(Exception(), StackTrace.current)\n    .onSuccess(\n        action: print,\n    )\n    .onFailure(\n        action: (failure) =\u003e print(\n            failure.error.toString(),\n        ),\n    );\n```\n\n**Methods available:**\n\n- `isSuccess`\n- `isFailure`\n- `getOrNull`\n- `failureOrNull`\n- `getOrThrow`\n- `getOrDefault`\n- `getOrElse`\n- `onSuccess`\n- `onFailure`\n- `fold`\n- `map`\n- `mapCatching`\n- `recover`\n- `recoverCatching`\n\nSee the [API documentation][api_documentation].\n\n### runCatching\n\nExecute a passed function and catches any `error` that is thrown from this execution, if the invocation is successful, then returns your `value` encapsulated in the `Result`, otherwise,  returns your `error` and `stack trace` encapsulated in the `Result`.\n\n```dart\nfinal result = runCatching(() =\u003e 1);\nresult\n    .onSuccess(\n        action: print,\n    )\n    .onFailure(\n        action: (failure) =\u003e print(\n            failure.error.toString(),\n        ),\n    );\n```\n\n**Note: You can be passed what `error` type you want catching, if the same, then return the `Result`, otherwise, the error is rethrown.**\n\n```dart\nfinal result = runCatching\u003cint\u003e(\n    () =\u003e throw 'exception',\n    test: (error) =\u003e error is Exception,\n),\n// `onFailure` is never called\nresult\n    .onFailure(\n        action: (failure) =\u003e print(\n            failure.error.toString(),\n        ),\n    );\n```\n\n### Stream\n\nTransform the stream value into a result:\n\n```dart\nfinal streamController = StreamController\u003cint\u003e();\nfinal resultStream = streamController.stream.toResult();\nresultStream.listen(print); // Success(0)\nstreamController.add(0);\n```\n\n**Like the `runCatching` you can be passed what `error` type you want transforming, if the same, then return the `Result`, otherwise, then the `error` and `StackTrace` are added using the `EventSink.addError`.**\n\n```dart\nfinal streamController = StreamController\u003cint\u003e();\nfinal resultStream = streamController.stream.toResult(\n    test: (error) =\u003e error is Exception,\n);\nresultStream.listen(print); // Failure(error: Exception, stackTrace: null)\nstreamController.addError(Exception());\n```\n\n## Other similar packages\n\nThe [dartz][dartz] package offers many functional programming helpers, including the Either type, which is similar to Result, with the difference being that it represents any two types of values.\n\nThe [fpdart][fpdart] similar to the package [dartz][dartz], however with a better documentation. Do you desire to use all the power of functional programming? If yes, then use it.\n\nThe [either_option][either_option] package has Either and Option and supports all of the typical functional operations.\n\nThe [result][result] package offers a few basic operations and may be adequate for simple cases\n(This package has been two years without any updates). \n\nThe [rust_like_result][rust_like_result] offers a simple Result type similar to the one in Rust.\n\nThe [simple_result][simple_result] package provides a Result type based on the type of the same name in Swift.\n\nThe [oxidized][oxidized] with types similar to those found in Rust, such as the Result which represents either a value or an error, and Option which either contains Some value or None.\n\n## 📝 Maintainers\n\n[Kauê Martins](https://github.com/kmartins)\n\n## 🤝 Support\n\nYou liked this package? Then give it a ⭐️. If you want to help then:\n\n- Fork this repository\n- Send a Pull Request with new features\n- Share this package\n- Create issues if you find a bug or want to suggest a new extension\n\n**Pull Request title follows [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/). \u003c/br\u003e**\n\n## 📝 License\n\nCopyright © 2022 [Kauê Martins](https://github.com/kmartins).\u003cbr /\u003e\nThis project is [MIT](https://opensource.org/licenses/MIT) licensed.\n\n[package_badge]: https://img.shields.io/pub/v/result_kt.svg\n[package_link]: https://pub.dev/packages/result_kt\n[license_badge]: https://img.shields.io/badge/license-MIT-blue.svg\n[license_link]: https://opensource.org/licenses/MIT\n[codecov_badge]: https://codecov.io/gh/kmartins/result_kt/branch/main/graph/badge.svg\n[codecov_link]: https://codecov.io/gh/kmartins/result_kt\n[workflow_badge]: https://github.com/kmartins/result_kt/actions/workflows/result_kt.yaml/badge.svg\n[workflow_link]: https://github.com/kmartins/result_kt/actions/workflows/result_kt.yaml\n[package]: https://pub.dev/packages/result_kt\n[kotlin_result]: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/\n[resultk]: https://pub.dev/packages/resultk\n[api_documentation]: https://pub.dev/documentation/result_kt/latest/\n[dartz]: https://pub.dev/packages/dartz\n[fpdart]: https://pub.dev/packages/fpdart\n[either_option]: https://pub.dev/packages/either_option\n[result]: https://pub.dev/packages/result\n[rust_like_result]: https://pub.dev/packages/rust_like_result\n[simple_result]: https://pub.dev/packages/simple_result\n[oxidized]: https://pub.dev/packages/oxidized","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkmartins%2Fresult_kt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkmartins%2Fresult_kt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkmartins%2Fresult_kt/lists"}