Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ Commands are `Listenable`, meaning you can react to their state changes:
#### Using `addListener`
```dart
fetchGreetingCommand.addListener(() {
final status = fetchGreetingCommand.value;
if (status is SuccessCommand<String>) {
print('Success: ${status.value}');
} else if (status is FailureCommand<String>) {
print('Failure: ${status.error}');
final state = fetchGreetingCommand.state;
if (state is SuccessCommand<String>) {
print('Success: ${state.data}');
} else if (state is FailureCommand<String>) {
print('Failure: ${state.error}');
}
});
```
Expand All @@ -67,7 +67,7 @@ Widget build(BuildContext context) {
builder: (context, _) {
return switch (state) {
RunningCommand<String>() => CircularProgressIndicator(),
SuccessCommand<String>(:final value) => Text('Success: $value'),
SuccessCommand<String>(:final data) => Text('Success: $data'),
FailureCommand<String>(:final error) => Text('Failure: $error'),
_ => ElevatedButton(
onPressed: () => fetchGreetingCommand.execute(),
Expand All @@ -83,12 +83,13 @@ Widget build(BuildContext context) {
The `when` method simplifies state management by mapping each state to a specific action or value:
```dart
fetchGreetingCommand.addListener(() {
final status = fetchGreetingCommand.value;
final message = status.when(
data: (value) => 'Success: $value',
final message = fetchGreetingCommand.state.when(
success: (data) => 'Success: $data',
failure: (exception) => 'Error: ${exception?.message}',
running: () => 'Fetching...',
orElse: () => 'Idle',
idle: () => 'Idle',
running: () => 'Running...',
cancelled: () => 'Cancelled',
orElse: () => 'Not provided state',
);

print(message);
Expand Down Expand Up @@ -146,7 +147,7 @@ To simplify interaction with commands, several helper methods and getters are av
#### State Check Getters
These getters allow you to easily check the current state of a command:
```dart
if (command.isRunning) {
if (command.state.isRunning) {
print('Command is idle and ready to execute.');
}
```
Expand Down Expand Up @@ -182,7 +183,7 @@ final filteredValue = command.filter<String>(
'Default Value',
(state) {
if (state is SuccessCommand<String>) {
return 'Success: ${state.value}';
return 'Success: ${state.data}';
} else if (state is FailureCommand<String>) {
return 'Error: ${state.error}';
}
Expand All @@ -199,7 +200,31 @@ This method simplifies state management by allowing you to focus on specific asp

---

### 7. CommandRef
### 7. Conditional State Handlers (`ifState` methods)

The `CommandState` class provides convenient methods to execute actions conditionally based on the current state. These methods are useful for side effects like navigation, showing snackbars, or logging.

#### Available Methods:
- **`ifIdle(action)`**: Executes the action if the state is `IdleCommand`.
- **`ifRunning(action)`**: Executes the action if the state is `RunningCommand`.
- **`ifSuccess(action)`**: Executes the action if the state is `SuccessCommand`, passing the `data`.
- **`ifFailure(action)`**: Executes the action if the state is `FailureCommand`, passing the `error`.
- **`ifCancelled(action)`**: Executes the action if the state is `CancelledCommand`.

#### Example:
```dart
await command.execute();

command.state
..ifSuccess((data) => print('Success: $data'))
..ifFailure((error) => showErrorSnackBar(error));
```

These methods provide a clean and readable way to handle specific states without requiring a full `when` or `switch` statement.

---

### 8. CommandRef

The `CommandRef` class allows you to create commands that listen to changes in one or more `ValueListenables` and execute actions based on derived values.

Expand All @@ -213,9 +238,9 @@ final commandRef = CommandRef<int, int>(
);

commandRef.addListener(() {
final status = commandRef.value;
final status = commandRef.state;
if (status is SuccessCommand<int>) {
print('Result: ${status.value}');
print('Result: ${status.data}');
}
});

Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class MyHomePage extends StatelessWidget {
valueListenable: incrementCommand,
builder: (context, state, snapshot) {
return FloatingActionButton(
onPressed: incrementCommand.isRunning
onPressed: incrementCommand.state.isRunning
? null
: () => incrementCommand.execute(),
tooltip: 'Increment',
Expand Down
26 changes: 13 additions & 13 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,26 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "6bb818ecbdffe216e81182c2f0714a2e62b593f4a4f13098713ff1685dfb6ab0"
sha256: "33e2e26bdd85a0112ec15400c8cbffea70d0f9c3407491f672a2fad47915e2de"
url: "https://pub.dev"
source: hosted
version: "10.0.9"
version: "11.0.2"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: f8b613e7e6a13ec79cfdc0e97638fddb3ab848452eff057653abd3edba760573
sha256: "1dbc140bb5a23c75ea9c4811222756104fbcd1a27173f0c34ca01e16bea473c1"
url: "https://pub.dev"
source: hosted
version: "3.0.9"
version: "3.0.10"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
sha256: "8d5a2d49f4a66b49744b23b018848400d23e54caf9463f4eb20df3eb8acb2eb1"
url: "https://pub.dev"
source: hosted
version: "3.0.1"
version: "3.0.2"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -127,10 +127,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394"
url: "https://pub.dev"
source: hosted
version: "1.16.0"
version: "1.17.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -203,18 +203,18 @@ packages:
dependency: transitive
description:
name: test_api
sha256: fb31f383e2ee25fbbfe06b40fe21e1e458d14080e3c67e7ba0acfde4df4e0bbd
sha256: ab2726c1a94d3176a45960b6234466ec367179b87dd74f1611adb1f3b5fb9d55
url: "https://pub.dev"
source: hosted
version: "0.7.4"
version: "0.7.7"
vector_math:
dependency: transitive
description:
name: vector_math
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
sha256: d530bd74fea330e6e364cda7a85019c434070188383e1cd8d9777ee586914c5b
url: "https://pub.dev"
source: hosted
version: "2.1.4"
version: "2.2.0"
vm_service:
dependency: transitive
description:
Expand All @@ -224,5 +224,5 @@ packages:
source: hosted
version: "15.0.0"
sdks:
dart: ">=3.7.0-0 <4.0.0"
dart: ">=3.8.0-0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
4 changes: 2 additions & 2 deletions lib/result_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
///- **`CancelledCommand`**: The action was explicitly stopped.
///
///### Accessing the State
///You can access the current state using the `value` property of the command:
///You can access the current state using the `state` property of the command:
///```dart
///final command = Command0<String>(() async {
/// return Success('Hello, World!');
///});
///
///// The current state of the command.
///print(command.value); // Outputs: SuccessCommand<String>
///print(command.state); // Outputs: SuccessCommand<String>
///```
///
///### Reacting to State Changes
Expand Down
Loading