{"id":17684597,"url":"https://github.com/mattrltrent/use_optimistic","last_synced_at":"2025-04-13T18:22:29.419Z","repository":{"id":224827740,"uuid":"764086800","full_name":"mattrltrent/use_optimistic","owner":"mattrltrent","description":"🪝 A port of React's useOptimistic hook.","archived":false,"fork":false,"pushed_at":"2024-02-27T23:35:36.000Z","size":433,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-06T13:16:51.744Z","etag":null,"topics":["flutter-hooks","hooks","package","pub-package","useoptimistic"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/use_optimistic","language":"C++","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/mattrltrent.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":"2024-02-27T13:06:49.000Z","updated_at":"2025-02-27T17:29:16.000Z","dependencies_parsed_at":"2024-02-27T23:45:08.406Z","dependency_job_id":null,"html_url":"https://github.com/mattrltrent/use_optimistic","commit_stats":null,"previous_names":["mattrltrent/use_optimistic"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattrltrent%2Fuse_optimistic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattrltrent%2Fuse_optimistic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattrltrent%2Fuse_optimistic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattrltrent%2Fuse_optimistic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattrltrent","download_url":"https://codeload.github.com/mattrltrent/use_optimistic/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248759135,"owners_count":21157096,"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":["flutter-hooks","hooks","package","pub-package","useoptimistic"],"created_at":"2024-10-24T10:24:07.272Z","updated_at":"2025-04-13T18:22:29.396Z","avatar_url":"https://github.com/mattrltrent.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flutter hook: `useOptimistic` 🪝\n\nAn easy-to-use hook to optimistically update generic state and then resolve it later (async or sync) by `accept`, `acceptAs`, or `reject`-ing.\n\n----\n\n### Documentation\n\n- [Full example](https://pub.dev/packages/use_optimistic/example) of using this hook to manage integer state.\n- GitHub repo found [here](https://github.com/mattrltrent/use_optimistic).\n- Package found [here](https://pub.dev/packages/use_optimistic).\n\nDocumentation is largely found in the package itself via doc-comments (*i.e. hover over package functions in your editor and it'll tell you what they do*). First, however, view the above [full example](https://pub.dev/packages/use_optimistic/example) to get started.\n\n### Simple usage\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/mattrltrent/use_optimistic/blob/main/assets/demo.png?raw=true\" style=\"height: 600px;\" alt=\"demo image\" /\u003e\n\u003c/p\u003e\n\n**Initialize the hook:**\n\n```dart\n  final UseOptimistic\u003cint\u003e useOptimistic = UseOptimistic\u003cint\u003e(initialState: 0)\n```\n\n**Ensure your widget listens to its state changes:**\n\n```dart\n@override\nvoid initState() {\n  super.initState();\n  useOptimistic.addListener(() =\u003e setState(() =\u003e debugPrint(\"state changed to: ${useOptimistic.state}\")));\n}\n```\n\n**Ensure the hook will be disposed when your widget is:**\n\n```dart\n@override\nvoid dispose() {\n  super.dispose();\n  useOptimistic.dispose();\n}\n```\n\n**Update your state optimistically:**\n\n```dart\nTextButton(\n  onPressed: () async {\n\n    Resolver r = useOptimistic.fn(\n      1, // the [newValue] to be passed to functions below\n      todo: (currentState, newValue) =\u003e currentState + newValue,\n      undo: (currentState, oldValue) =\u003e currentState - oldValue,\n    );\n\n    // simulating an API call\n    await Future.delayed(const Duration(seconds: 1));\n\n    // three mutually exclusive ways to deal with result\n    r.acceptAs(2); // [undo] original function, then [todo] with new value\n    r.accept(); // accept the original optimistic update\n    r.reject(); // reject the original optimistic update and [undo] original function\n\n  },\n  child: const Text(\"optimistically add 1\"),\n),\n```\n\nYou can call `useOptimistic.fn( ... )` multiple times with different `todo` and `undo` functions and it'll execute the proper `todo`/`undo` associated with `fn` at the moment you called it. This means you can call multiple *separate* `useOptimistic.fn( ... )`s safely together. It does *not mean* you can have a single `fn` like this (pseudo-code): `useOptimistic.fn( if x todo: () =\u003e {...} else todo: () =\u003e {...} )` that has conditionally rendered `todo`/`undo` functions.\n\n**Listen to the state:**\n\n```dart\nText(\"current value: ${useOptimistic.state}\"),\n```\n\n### Extra\n\n- The package is always open to [improvements](https://github.com/mattrltrent/use_optimistic/issues), [suggestions](mailto:me@matthewtrent.me), and [additions](https://github.com/mattrltrent/use_optimistic/pulls)!\n- I'll look through GitHub PRs and Issues as soon as I can.\n\n![analytics](https://hidden-coast-90561-45544df95b1b.herokuapp.com/api/v1/analytics/?kind=package-use-optimistic)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattrltrent%2Fuse_optimistic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattrltrent%2Fuse_optimistic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattrltrent%2Fuse_optimistic/lists"}