Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 9d7b276

Browse files
committed
adding onSuccess builder and isSuccess property
1 parent e17b1ce commit 9d7b276

5 files changed

Lines changed: 36 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[7.2.0] - 25.12.2024
2+
* adding `onSuccess` builder to the `CommandBuilder` and `isSuccess` to the `CommandResults` to make them easiery to use
3+
if the Command doesn't return a value.
14
[7.1.0] 27.11.2024
25
* adding `isExecutingSync` to allow better chaining of commands
36
[7.0.1] 27.11.2024

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,10 @@ class CommandResult<TParam, TResult> {
311311
final Object error;
312312
final bool isExecuting;
313313
314+
bool get isSuccsess => !hasError && !isExecuting;
314315
bool get hasData => data != null;
315316
bool get hasError => error != null;
317+
316318
317319
/// This is a stripped down version of the class. Please see the source
318320
}
@@ -378,6 +380,9 @@ child: CommandBuilder<String, List<WeatherEntry>>(
378380
),
379381
```
380382

383+
In case your Command does not return a value you can use the `onSuccess` builder.
384+
385+
381386
### toWidget() extension method on Command Result
382387
I you are using a package `get_it_mixin`, `provider` or `flutter_hooks` you probably don't want to use the `CommandBuilder` for you there is an extension method for the `CommandResult` type that you can use like this:
383388

lib/command_builder.dart

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
import 'package:flutter/widgets.dart';
2-
import 'package:flutter_command/flutter_command.dart';
1+
part of flutter_command;
32

43
class CommandBuilder<TParam, TResult> extends StatelessWidget {
54
final Command<TParam, TResult> command;
5+
6+
/// This builder will be called when the
7+
/// command is executed successfully, independent of the return value.
8+
final Widget Function(BuildContext context, TParam? param)? onSuccess;
9+
10+
/// If your command has a return value, you can use this builder to build a widget
11+
/// when the command is executed successfully.
612
final Widget Function(BuildContext context, TResult data, TParam? param)?
713
onData;
14+
15+
/// If the command has no return value or returns null, this builder will be called when the
16+
/// command is executed successfully.
817
final Widget Function(BuildContext context, TParam? param)? onNullData;
918
final Widget Function(
1019
BuildContext context,
@@ -20,6 +29,7 @@ class CommandBuilder<TParam, TResult> extends StatelessWidget {
2029

2130
const CommandBuilder({
2231
required this.command,
32+
this.onSuccess,
2333
this.onData,
2434
this.onNullData,
2535
this.whileExecuting,
@@ -29,10 +39,13 @@ class CommandBuilder<TParam, TResult> extends StatelessWidget {
2939

3040
@override
3141
Widget build(BuildContext context) {
42+
if (command._noReturnValue) {}
3243
return ValueListenableBuilder<CommandResult<TParam?, TResult>>(
3344
valueListenable: command.results,
3445
builder: (context, result, _) {
3546
return result.toWidget(
47+
onSuccess: (paramData) =>
48+
onSuccess?.call(context, paramData) ?? const SizedBox(),
3649
onData: (data, paramData) =>
3750
onData?.call(context, data, paramData) ?? const SizedBox(),
3851
onNullData: (paramData) =>
@@ -59,19 +72,25 @@ class CommandBuilder<TParam, TResult> extends StatelessWidget {
5972
extension ToWidgeCommandResult<TParam, TResult>
6073
on CommandResult<TParam, TResult> {
6174
Widget toWidget({
62-
required Widget Function(TResult result, TParam? param) onData,
75+
Widget Function(TResult result, TParam? param)? onData,
76+
Widget Function(TParam? param)? onSuccess,
6377
Widget Function(TParam? param)? onNullData,
6478
Widget Function(TResult? lastResult, TParam? param)? whileExecuting,
6579
Widget Function(Object error, TResult? lastResult, TParam? param)? onError,
6680
}) {
81+
assert(onData != null || onSuccess != null,
82+
'You have to provide at least a builder for onData or onSuccess');
6783
if (error != null) {
6884
return onError?.call(error!, data, paramData) ?? const SizedBox();
6985
}
7086
if (isExecuting) {
7187
return whileExecuting?.call(data, paramData) ?? const SizedBox();
7288
}
89+
if (onSuccess != null) {
90+
return onSuccess.call(paramData);
91+
}
7392
if (data != null) {
74-
return onData(data as TResult, paramData);
93+
return onData?.call(data as TResult, paramData) ?? const SizedBox();
7594
} else {
7695
return onNullData?.call(paramData) ?? const SizedBox();
7796
}

lib/flutter_command.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import 'package:stack_trace/stack_trace.dart';
1111

1212
import 'error_filters.dart';
1313

14-
export 'package:flutter_command/command_builder.dart';
1514
export 'package:flutter_command/error_filters.dart';
1615
export 'package:functional_listener/functional_listener.dart';
1716

1817
part './async_command.dart';
1918
part './mock_command.dart';
2019
part './sync_command.dart';
2120
part './undoable_command.dart';
21+
part './command_builder.dart';
2222

2323
/// Combined execution state of a `Command` represented using four of its fields.
2424
/// A [CommandResult] will be issued for any state change of any of its fields
@@ -52,6 +52,9 @@ class CommandResult<TParam, TResult> {
5252

5353
const CommandResult.blank() : this(null, null, null, false);
5454

55+
/// if a CommandResult is not executing and has no error, it is considered successful
56+
/// if the command has no return value, this can be used to check if the command was executed successfully
57+
bool get isSuccess => !isExecuting && !hasError;
5558
bool get hasData => data != null;
5659

5760
bool get hasError => error != null && !isUndoValue;

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_command
22
description: flutter_command is a way to manage your state based on `ValueListenable` and the `Command` design pattern.
3-
version: 7.1.0
3+
version: 7.2.0
44
homepage: https://github.com/escamoteur/flutter_command
55

66
environment:

0 commit comments

Comments
 (0)