Search packages, repositories and app categories

Package detailsv1.0.2

use_optimistic

An easy-to-use hook to optimistically update generic state and then resolve it later asynchronously by `accept`, `acceptAs`, or `reject`-ing.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
10
Likes
5
Pub points
160

Install package

flutter pub add use_optimistic

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

A Flutter/Dart package providing a simple hook for optimistically updating state. It allows developers to immediately reflect changes in the UI, then resolve them asynchronously using accept, acceptAs, or reject methods. Ideal for scenarios like form submissions, toggles, or any action where immediate feedback improves UX before server confirmation. The hook manages state transitions smoothly and supports multiple platforms.

Use cases

  • Real-time form updates
  • Toggle actions with immediate feedback
  • Async operations with optimistic UI
  • Data submission with rollback capability
  • User interaction simulations

Key features

  • Optimistic state updates
  • Support for accept/acceptAs/reject
  • Simple API with minimal boilerplate
  • Cross-platform compatibility
  • Built-in state management
  • Easy integration with existing hooks

Suitable for

  • Developers seeking instant UI feedback
  • Projects requiring temporary state changes
  • Apps with frequent async interactions
  • Teams prioritizing UX responsiveness
  • Flutter apps using functional widgets

Considerations

  • Requires understanding of async state handling
  • Not suitable for critical data without fallbacks
  • Needs proper error handling when rejecting
  • Best used with clear user feedback mechanisms
  • May require additional logic for complex workflows

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

Flutter hook: useOptimistic 🪝

An easy-to-use hook to optimistically update generic state and then resolve it later (async or sync) by accept, acceptAs, or reject-ing.


Documentation
  • Full example of using this hook to manage integer state.
  • GitHub repo found here.
  • Package found here.

Documentation 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 to get started.

Simple usage

demo image

Initialize the hook:

  final UseOptimistic<int> useOptimistic = UseOptimistic<int>(initialState: 0)

Ensure your widget listens to its state changes:

@override
void initState() {
  super.initState();
  useOptimistic.addListener(() => setState(() => debugPrint("state changed to: ${useOptimistic.state}")));
}

Ensure the hook will be disposed when your widget is:

@override
void dispose() {
  super.dispose();
  useOptimistic.dispose();
}

Update your state optimistically:

TextButton(
  onPressed: () async {

    Resolver r = useOptimistic.fn(
      1, // the [newValue] to be passed to functions below
      todo: (currentState, newValue) => currentState + newValue,
      undo: (currentState, oldValue) => currentState - oldValue,
    );

    // simulating an API call
    await Future.delayed(const Duration(seconds: 1));

    // three mutually exclusive ways to deal with result
    r.acceptAs(2); // [undo] original function, then [todo] with new value
    r.accept(); // accept the original optimistic update
    r.reject(); // reject the original optimistic update and [undo] original function

  },
  child: const Text("optimistically add 1"),
),

You 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: () => {...} else todo: () => {...} ) that has conditionally rendered todo/undo functions.

Listen to the state:

Text("current value: ${useOptimistic.state}"),
Extra
  • The package is always open to improvements, [suggestions](mailto:me@matthewtrent.me), and additions!
  • I'll look through GitHub PRs and Issues as soon as I can.

analytics

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package

No data available yet.