{"id":16583876,"url":"https://github.com/aloisdeniel/dart_maybe","last_synced_at":"2025-09-09T13:46:19.521Z","repository":{"id":53908580,"uuid":"157758604","full_name":"aloisdeniel/dart_maybe","owner":"aloisdeniel","description":"No more null check with an dart equivalent of Maybe (Haskel, Elm) / Option (F#).","archived":false,"fork":false,"pushed_at":"2021-03-11T18:49:20.000Z","size":18,"stargazers_count":19,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T05:41:28.554Z","etag":null,"topics":["dart","functional","maybe","option"],"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/aloisdeniel.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2018-11-15T18:59:49.000Z","updated_at":"2024-06-03T19:27:40.000Z","dependencies_parsed_at":"2022-08-13T04:00:58.634Z","dependency_job_id":null,"html_url":"https://github.com/aloisdeniel/dart_maybe","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/aloisdeniel/dart_maybe","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aloisdeniel%2Fdart_maybe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aloisdeniel%2Fdart_maybe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aloisdeniel%2Fdart_maybe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aloisdeniel%2Fdart_maybe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aloisdeniel","download_url":"https://codeload.github.com/aloisdeniel/dart_maybe/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aloisdeniel%2Fdart_maybe/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274306335,"owners_count":25261038,"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","status":"online","status_checked_at":"2025-09-09T02:00:10.223Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","functional","maybe","option"],"created_at":"2024-10-11T22:43:25.200Z","updated_at":"2025-09-09T13:46:19.437Z","avatar_url":"https://github.com/aloisdeniel.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# maybe\n\n[![Pub](https://img.shields.io/pub/v/maybe.svg)](https://pub.dartlang.org/packages/maybe)\n\nNo more null check with an dart equivalent of Maybe (Haskel, Elm) / Option (F#).\n\n## Usage\n\nThe key is that you need to call the `some` or `when` to access your potential value so you are forced to check its status before using it.\n\n### `Maybe\u003cT\u003e.nothing` : creating an optional item that is empty\n\n```dart\nfinal maybe = Maybe\u003cString\u003e.nothing();\n```\n\n### `Maybe.some` : creating an optional item with a value\n\n```dart\nfinal maybe = Maybe.some(\"hello world\");\n```\n\n```dart\nfinal isNothing = Maybe\u003cString\u003e.some(null); // By default `some` with a null value is converted to `nothing`\nfinal isNotNothing = Maybe\u003cString\u003e.some(null, nullable: true);\n```\n\n### `some` : extracting some value\n\n```dart\nfinal maybe = Maybe.some(\"hello world\");\nfinal value = some(maybe, \"default\"); // == \"hello world\"\n```\n\n```dart\nfinal maybe = Maybe\u003cString\u003e.nothing();\nfinal value = some(maybe, \"default\"); // == \"default\"\n```\n\n### `isNothing` : testing if some value\n\n```dart\nfinal maybe = Maybe.some(\"hello world\");\nfinal value = isNothing(maybe); // false\n```\n\n```dart\nfinal maybe = Maybe\u003cString\u003e.nothing();\nfinal value = isNothing(maybe); // true\n```\n\n### `when` : triggering an action\n\n```dart\nvar maybe = Maybe.some(\"hello world\");\nwhen(maybe, some: (v) {\n    print(v); // \"hello world\"\n});\n\n// Defining nothing\nmaybe = Maybe.nothing();\nwhen(maybe, some: (v) {\n    print(v); // not called!\n});\n\n// You can add a default value when nothing\nmaybe = Maybe\u003cString\u003e.some(null);\nwhen(maybe, some: (v) {\n        print(v); // \"hello world\"\n    }, \n    defaultValue: () =\u003e \"hello world\");\n```\n\n### `mapSome` : converts a value type to another\n\n```dart\nvar maybe = Maybe.some(\"hello world\");\nvar converted = mapSome\u003cString,int\u003e(maybe, (v) =\u003e v.length);\nvar value = some(converted,0); // == 11\n```\n\n```dart\nvar maybe = Maybe\u003cString\u003e.nothing();\nvar converted = mapSome\u003cString,int\u003e(maybe, (v) =\u003e v.length);\nvar value = some(converted, 0); // == 0\n```\n\n### `MaybeMap\u003cK,V\u003e` : a map with optional values (*aka Map\u003cK, Maybe\u003cV\u003e\u003e*)\n\n```dart\nvar map = MaybeMap\u003cString,String\u003e();\nmap[\"test\"] = Maybe.nothing(); // doesn't add value\nmap[\"test\"] = Maybe.some(\"value\"); // adds value\nwhen(map[\"test\"], some: (v) =\u003e print(v));\n\nmap[\"test\"] = Maybe.nothing(); // deletes key\nwhen(map[\"test\"], isNothing: (v) =\u003e print(\"deleted :\" + map.containsKey(\"test\").toString()));\n```\n\n```dart\nMap\u003cString,String\u003e maybeMap = {\n    \"test\": \"value\",\n};\nvar maybeMap = MaybeMap\u003cString,String\u003e.fromMap(maybeMap);\nwhen(map[\"test\"], some: (v) =\u003e print(v));\n```\n\n## What about quiver's `Optional` ?\n\nThe [Optional](https://github.com/google/quiver-dart/blob/master/lib/src/core/optional.dart) type has several similarities with `Maybe`, but there are several subtle differences.\n\n### Optional can be `null`\n\nLet's take a quick example :\n\n```dart\nclass Update {\n  final Optional\u003cString\u003e title;\n  final Optional\u003cString\u003e description;\n\n  Update({Optional\u003cString\u003e title, Optional\u003cString\u003e description})\n      : this.title = title ?? Optional\u003cString\u003e.absent(),\n        this.description = description ?? Optional\u003cString\u003e.absent();\n}\n\nfinal update = Update(title: Optional.of('sample'));\n\nupdate.title.ifPresent((v) {\n    print('title: $v');\n});\n\nupdate.description.ifPresent((v) {\n    print('description: $v');\n});\n```\n\nThanks to static functions, all can be replaced by :\n\n```dart\nclass Update {\n  final Maybe\u003cString\u003e title;\n  final Maybe\u003cString\u003e description;\n\n  Update({this.title this.description});\n}\n\nfinal update = Update(title: Maybe.some('sample'));\n\nwhen(update.title, some: (v) {\n    print('title: $v');\n});\n\nwhen(update.description, some: (v) {\n    print('description: $v');\n});\n```\n\nSo, the critical part is that you can forget that `Optional` can be `null` itself and produce exceptions (`update.title.ifPresent` in our example). You are then forced to test its nullity and you come back to the initial problematic. This is where `Maybe` feels more robust to me.\n\n### `absent` is similar to `null`\n\nWith `Maybe`, values can be nullable.\n\nIn the following example, we explicitly say that the title should have a new `null` value.\n\n```dart\nclass Update {\n  final Maybe\u003cString\u003e title;\n  final Maybe\u003cString\u003e description;\n\n  Update({ this.title, this.description});\n}\n\nfinal update = Update(title: Maybe.some(null, nullable: true);\n```\n\nThis is really different than having a `nothing` title, which significates that the title shouldn't be modified.\n\n```dart\nfinal update = Update(title: Maybe.nothing());\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faloisdeniel%2Fdart_maybe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faloisdeniel%2Fdart_maybe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faloisdeniel%2Fdart_maybe/lists"}