π Complete documentation available at flutter-it.dev Check out the comprehensive docs with detailed guides, examples, and best practices!
Important: Version 9.0.0 introduces new, clearer API naming. The old API is deprecated and will be removed in v10.0.0.
Quick Migration:
execute()βrun()|isExecutingβisRunning|canExecuteβcanRunRun
dart fix --applyto automatically update most usages.
Command pattern for Flutter - wrap functions as observable objects with automatic state management
Commands replace async methods with reactive alternatives. Wrap your functions, get automatic loading states, error handling, and UI integration. No manual state tracking, no try/catch everywhere.
Call them like functions. React to their state. Simple as that.
Part of flutter_it β A construction set of independent packages. command_it works standalone or combines with watch_it for reactive UI updates.
- π― Declarative β Wrap functions, get observable execution state automatically
- β‘ Automatic State β isRunning, value, errors tracked without manual code
- π‘οΈ Smart Error Handling β Route errors globally/locally with filters
- π Built-in Protection β Prevents parallel execution automatically
- ποΈ Restrictions β Disable commands conditionally (auth, network, etc.)
- π§ͺ Testable β Easier to test than traditional async methods
Learn more about the benefits β
Add to your pubspec.yaml:
dependencies:
command_it: ^9.0.2
listen_it: ^5.3.3 # Required - commands build on ValueListenableimport 'package:command_it/command_it.dart';
// 1. Create a command that wraps your async function
class CounterManager {
int _counter = 0;
late final incrementCommand = Command.createAsyncNoParam<String>(
() async {
await Future.delayed(Duration(milliseconds: 500));
_counter++;
return _counter.toString();
},
initialValue: '0',
);
}
// 2. Use it in your UI - command is a ValueListenable
class CounterWidget extends StatelessWidget {
final manager = CounterManager();
@override
Widget build(BuildContext context) {
return Column(
children: [
// Shows loading indicator automatically while command runs
ValueListenableBuilder<bool>(
valueListenable: manager.incrementCommand.isRunning,
builder: (context, isRunning, _) {
if (isRunning) return CircularProgressIndicator();
return ValueListenableBuilder<String>(
valueListenable: manager.incrementCommand,
builder: (context, value, _) => Text('Count: $value'),
);
},
),
ElevatedButton(
onPressed: manager.incrementCommand.run,
child: Text('Increment'),
),
],
);
}
}That's it! The command automatically:
- Prevents parallel execution
- Tracks isRunning state
- Publishes results
- Handles errors
Simplify your UI code with the built-in builder widget:
CommandBuilder<void, String>(
command: manager.incrementCommand,
whileRunning: (context, _, __) => CircularProgressIndicator(),
onData: (context, value, _) => Text('Count: $value'),
onError: (context, error, _, __) => Text('Error: $error'),
)Create commands for any function signature:
- createAsync β Async with parameter and result
- createAsyncNoParam β Async without parameter
- createAsyncNoResult β Async that returns nothing
- createSync β Sync with parameter and result
- Plus NoParam and NoResult variants for sync commands
Observe different aspects of execution:
- value β Last successful result
- isRunning β Async execution state
- isRunningSync β Synchronous version for restrictions
- canRun β Combined restriction and running state
- errors β Stream of errors
- results β CommandResult with all data at once
Declarative error routing with filters:
- Basic Error Handling β Listen to errors locally
- Global Handler β App-wide error handling
- Global Errors Stream β Reactive monitoring of all globally-routed errors
- Error Filters β Route errors by type or predicate
- Built-in Filters β GlobalIfNoLocalErrorFilter, PredicatesErrorFilter, etc.
- Command Restrictions β Disable commands conditionally
- CommandBuilder β Widget for simpler UI code
- Undoable Commands β Built-in undo/redo support
- Command Piping β Chain commands together automatically
- Testing β Patterns for testing commands
Chain commands together with the pipeToCommand() extension. When the source completes successfully, it automatically triggers the target command:
// Trigger refresh after save completes
saveCommand.pipeToCommand(refreshCommand);
// Transform result before passing to target
userIdCommand.pipeToCommand(fetchUserCommand, transform: (id) => UserRequest(id));
// Pipe from any ValueListenable - track execution state changes
longRunningCommand.isRunning.pipeToCommand(spinnerStateCommand);The pipeToCommand() extension works on any ValueListenable, including commands, isRunning, results, or plain ValueNotifier. Returns a ListenableSubscription for manual cancellation if needed.
β οΈ Warning: Circular pipes (AβBβA) cause infinite loops. Ensure your pipe graph is acyclic.
Built on listen_it β Commands are ValueListenable objects, so they work with all listen_it operators (map, debounce, where, etc.).
// Register with get_it
di.registerLazySingleton(() => TodoManager());
// Use commands in your managers
class TodoManager {
final loadTodosCommand = Command.createAsyncNoParam<List<Todo>>(
() => api.fetchTodos(),
[],
);
// Debounce search with listen_it operators
final searchCommand = Command.createSync<String, String>((s) => s, '');
TodoManager() {
searchCommand.debounce(Duration(milliseconds: 500)).listen((term, _) {
loadTodosCommand.run();
});
}
}Want more? Combine with other flutter_it packages:
-
listen_it β Required dependency. ValueListenable operators and reactive collections.
-
Optional: watch_it β State management. Watch commands reactively without builders:
watchValue((m) => m.loadCommand). -
Optional: get_it β Service locator for dependency injection. Access managers with commands from anywhere:
di<TodoManager>().
π‘ flutter_it is a construction set β command_it works standalone. Add watch_it and get_it when you need reactive UI and dependency injection.
- Getting Started Guide β Installation, concepts, first command
- Command Basics β Creating and running commands
- Command Properties β value, isRunning, canRun, errors, results
- Command Types β Choosing the right factory function
- Error Handling β Basic error patterns
- Error Filters β Advanced error routing
- Command Restrictions β Conditional execution control
- Command Builders β Simplifying UI code
- Testing Commands β Test patterns and examples
- Integration with watch_it β Reactive UI updates
- Best Practices β Patterns, anti-patterns, tips
- Discord β Get help, share ideas, connect with other developers
- GitHub Issues β Report bugs, request features
- GitHub Discussions β Ask questions, share patterns
Contributions are welcome! Please read the contributing guidelines before submitting PRs.
MIT License - see LICENSE file for details.
Part of the flutter_it ecosystem β Build reactive Flutter apps the easy way. No codegen, no boilerplate, just code.
