RxCommand<TParam, TResult> class abstract

RxCommand capsules a given handler function that can then be executed by its execute method. The result of this method is then published through its Observable (Observable wrap Dart Streams) Additionally it offers Observables for it's current execution state, if the command can be executed and for all possibly thrown exceptions during command execution.

RxCommand implements the Observable interface so you can listen directly to the RxCommand which emits the results of the wrapped function. If this function has a void return type it will still output one void item so that you can listen for the end of the execution.

The results Observable emits CommandResult<TRESULT> which is often easier in combination with Flutter StreamBuilder because you have all state information at one place.

An RxCommand is a generic class of type RxCommand<TParam, TRESULT> where TParam is the type of data that is passed when calling execute and TResult denotes the return type of the handler function. To signal that a handler doesn't take a parameter or returns no value use the type void

Inheritance
Implementers

Constructors

RxCommand(Subject<TResult> _resultsSubject, Stream<bool?>? restriction, bool _includeLastResultInCommandResults, bool _resultSubjectIsBehaviourSubject, TResult? lastResult, bool noReturnValue, String? debugName, bool noParamValue)

Properties

canExecute Stream<bool>
Observable stream that issues a bool on any change of the current executable state of the command. Meaning if the command cann be executed or not. This will issue false while the command executes but also if the command receives a false from the canExecute Observable that you can pass when creating the Command
no setter
first Future<TResult>
The first element of this stream.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
isBroadcast bool
Whether this stream is a broadcast stream.
no setterinherited
isEmpty Future<bool>
Whether this stream contains any elements.
no setterinherited
isExecuting Stream<bool>
Observable stream that issues a bool on any execution state change of the command
no setter
last Future<TResult>
The last element of this stream.
no setterinherited
lastResult ↔ TResult?
The result of the last successful call to execute. This is especially handy to use as initialData of Flutter StreamBuilder
getter/setter pair
length Future<int>
The number of elements in this stream.
no setterinherited
next Future<TResult>
This property is a utility which allows us to chain RxCommands together.
no setter
results Stream<CommandResult<TParam, TResult>>
emits CommandResult<TRESULT> the combined state of the command, which is often easier in combination with Flutter StreamBuilder because you have all state information at one place.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
single Future<TResult>
The single element of this stream.
no setterinherited
throwExceptions bool
By default RxCommand will catch all exceptions during execution of the command. And publish them on .thrownExceptions and in the CommandResult. If don't want this and have exceptions thrown, set this to true.
getter/setter pair
thrownExceptions Stream
When subribing to thrownExceptionsyou will every excetpion that was thrown in your handler function as an event on this Observable. If no subscription exists the Exception will be rethrown
no setter

Methods

any(bool test(TResult element)) Future<bool>
Checks whether test accepts any element provided by this stream.
inherited
asBroadcastStream({void onListen(StreamSubscription<TResult> subscription)?, void onCancel(StreamSubscription<TResult> subscription)?}) Stream<TResult>
Returns a multi-subscription stream that produces the same events as this.
inherited
asyncExpand<E>(Stream<E>? convert(TResult event)) Stream<E>
Transforms each element into a sequence of asynchronous events.
inherited
asyncMap<E>(FutureOr<E> convert(TResult event)) Stream<E>
Creates a new stream with each data event of this stream asynchronously mapped to a new event.
inherited
call([TParam? param]) → void
This makes RxCommand a callable class, so instead of myCommand.execute() you can write myCommand()
cast<R>() Stream<R>
Adapt this stream to be a Stream<R>.
inherited
contains(Object? needle) Future<bool>
Returns whether needle occurs in the elements provided by this stream.
inherited
dispose() → void
If you don't need a command any longer it is a good practise to dispose it to make sure all stream subscriptions are cancelled to prevent memory leaks
distinct([bool equals(TResult previous, TResult next)?]) Stream<TResult>
Skips data events if they are equal to the previous data event.
inherited
drain<E>([E? futureValue]) Future<E>
Discards all data on this stream, but signals when it is done or an error occurred.
inherited
elementAt(int index) Future<TResult>
Returns the value of the indexth data event of this stream.
inherited
every(bool test(TResult element)) Future<bool>
Checks whether test accepts all elements provided by this stream.
inherited
execute([TParam? param]) → void
Calls the wrapped handler function with an option input parameter
expand<S>(Iterable<S> convert(TResult element)) Stream<S>
Transforms each element of this stream into a sequence of elements.
inherited
firstWhere(bool test(TResult element), {TResult orElse()?}) Future<TResult>
Finds the first element of this stream matching test.
inherited
fold<S>(S initialValue, S combine(S previous, TResult element)) Future<S>
Combines a sequence of values by repeatedly applying combine.
inherited
forEach(void action(TResult element)) Future<void>
Executes action on each element of this stream.
inherited
handleError(Function onError, {bool test(dynamic error)?}) Stream<TResult>
Creates a wrapper Stream that intercepts some errors from this stream.
inherited
join([String separator = ""]) Future<String>
Combines the string representation of elements into a single string.
inherited
lastWhere(bool test(TResult element), {TResult orElse()?}) Future<TResult>
Finds the last element in this stream matching test.
inherited
listen(void onData(TResult value)?, {Function? onError, void onDone()?, bool? cancelOnError}) StreamSubscription<TResult>
Adds a subscription to this stream.
inherited
map<S>(S convert(TResult event)) Stream<S>
Transforms each element of this stream into a new stream event.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pipe(StreamConsumer<TResult> streamConsumer) Future
Pipes the events of this stream into streamConsumer.
inherited
reduce(TResult combine(TResult previous, TResult element)) Future<TResult>
Combines a sequence of values by repeatedly applying combine.
inherited
singleWhere(bool test(TResult element), {TResult orElse()?}) Future<TResult>
Finds the single element in this stream matching test.
inherited
skip(int count) Stream<TResult>
Skips the first count data events from this stream.
inherited
skipWhile(bool test(TResult element)) Stream<TResult>
Skip data events from this stream while they are matched by test.
inherited
take(int count) Stream<TResult>
Provides at most the first count data events of this stream.
inherited
takeWhile(bool test(TResult element)) Stream<TResult>
Forwards data events while test is successful.
inherited
timeout(Duration timeLimit, {void onTimeout(EventSink<TResult> sink)?}) Stream<TResult>
Creates a new stream with the same events as this stream.
inherited
toList() Future<List<TResult>>
Collects all elements of this stream in a List.
inherited
toSet() Future<Set<TResult>>
Collects the data of this stream in a Set.
inherited
toString() String
A string representation of this object.
inherited
transform<S>(StreamTransformer<TResult, S> streamTransformer) Stream<S>
Applies streamTransformer to this stream.
inherited
where(bool test(TResult event)) Stream<TResult>
Creates a new stream from this stream that discards some elements.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

globalExceptionHandler ↔ (void Function(String? commandName, CommandError<Object> error)?)
optional hander that will get called on any exception that happens inside any Command of the app. Ideal for logging. commandName the debugName of the Command
getter/setter pair
loggingHandler ↔ (void Function(String? commandName, CommandResult result)?)
optional handler that will get called on all Command executions. commandName the debugName of the Command
getter/setter pair

Static Methods

createAsync<TParam, TResult>(AsyncFunc1<TParam, TResult> func, {Stream<bool?>? restriction, bool emitInitialCommandResult = false, bool emitLastResult = false, bool emitsLastValueToNewSubscriptions = false, TResult? initialLastResult, String? debugName}) RxCommand<TParam, TResult>
Creates a RxCommand for an asynchronous handler function with parameter that returns a value func: handler function canExecute : observable that can be used to enable/disable the command based on some other state change if omitted the command can be executed always except it's already executing isExecuting will issue a bool value on each state change. Even if you subscribe to a newly created command it will issue false For the Observable<CommandResult> that RxCommand publishes in results this normally doesn't make sense if you want to get an initial Result with data==null, error==null, isExecuting==false pass emitInitialCommandResult=true. emitLastResult will include the value of the last successful execution in all CommandResult events unless there is no result. By default the results Observable and the RxCommand itself behave like a PublishSubject. If you want that it acts like a BehaviourSubject, meaning every listener gets the last received value, you can set emitsLastValueToNewSubscriptions = true. initialLastResult sets the value of the lastResult property before the first item was received. This is helpful if you use lastResult as initialData of a StreamBuilder debugName optional identifier that is included when you register a globalExceptionHandler or a loggingHandler
createAsyncNoParam<TResult>(AsyncFunc<TResult> func, {Stream<bool>? restriction, bool emitInitialCommandResult = false, bool emitLastResult = false, bool emitsLastValueToNewSubscriptions = false, TResult? initialLastResult, String? debugName}) RxCommand<void, TResult>
Creates a RxCommand for an asynchronous handler function with no parameter that returns a value func: handler function canExecute : observable that can be used to enable/disable the command based on some other state change if omitted the command can be executed always except it's already executing isExecuting will issue a bool value on each state change. Even if you subscribe to a newly created command it will issue false for the Observable<CommandResult> that RxCommand publishes in results this normally doesn't make sense if you want to get an initial Result with data==null, error==null, isExecuting==false pass emitInitialCommandResult=true. emitLastResult will include the value of the last successful execution in all CommandResult events unless there is no result. By default the results Observable and the RxCommand itself behave like a PublishSubject. If you want that it acts like a BehaviourSubject, meaning every listener gets the last received value, you can set emitsLastValueToNewSubscriptions = true. initialLastResult sets the value of the lastResult property before the first item was received. This is helpful if you use lastResult as initialData of a StreamBuilder debugName optional identifier that is included when you register a globalExceptionHandler or a loggingHandler
createAsyncNoParamNoResult(AsyncAction action, {Stream<bool>? restriction, bool emitInitialCommandResult = false, bool emitsLastValueToNewSubscriptions = false, String? debugName}) RxCommand<void, void>
Creates a RxCommand for an asynchronous handler function with no parameter and no return type action: handler function canExecute : observable that can be used to enable/disable the command based on some other state change if omitted the command can be executed always except it's already executing isExecuting will issue a bool value on each state change. Even if you subscribe to a newly created command it will issue false For the Observable<CommandResult> that RxCommand implement this normally doesn't make sense if you want to get an initial Result with data==null, error==null, isExecuting==false pass emitInitialCommandResult=true. By default the results Observable and the RxCommand itself behave like a PublishSubject. If you want that it acts like a BehaviourSubject, meaning every listener gets the last received value, you can set emitsLastValueToNewSubscriptions = true. debugName optional identifier that is included when you register a globalExceptionHandler or a loggingHandler
createAsyncNoResult<TParam>(AsyncAction1<TParam> action, {Stream<bool>? restriction, bool emitInitialCommandResult = false, bool emitsLastValueToNewSubscriptions = false, String? debugName}) RxCommand<TParam, void>
Creates a RxCommand for an asynchronous handler function with one parameter and no return type action: handler function canExecute : observable that can be used to enable/disable the command based on some other state change if omitted the command can be executed always except it's already executing isExecuting will issue a bool value on each state change. Even if you subscribe to a newly created command it will issue false For the Observable<CommandResult> that RxCommand implement this normally doesn't make sense if you want to get an initial Result with data==null, error==null, isExecuting==false pass emitInitialCommandResult=true. By default the results Observable and the RxCommand itself behave like a PublishSubject. If you want that it acts like a BehaviourSubject, meaning every listener gets the last received value, you can set emitsLastValueToNewSubscriptions = true. debugName optional identifier that is included when you register a globalExceptionHandler or a loggingHandler
createFromStream<TParam, TResult>(StreamProvider<TParam, TResult> provider, {Stream<bool>? restriction, bool emitInitialCommandResult = false, bool emitLastResult = false, bool emitsLastValueToNewSubscriptions = false, TResult? initialLastResult, String? debugName}) RxCommand<TParam, TResult>
Creates a RxCommand from an "one time" observable. This is handy if used together with a streame generator function. provider: provider function that returns a Observable that will be subscribed on the call of execute restriction : observable that can be used to enable/disable the command based on some other state change if omitted the command can be executed always except it's already executing isExecuting will issue a bool value on each state change. Even if you subscribe to a newly created command it will issue false For the Observable<CommandResult> that RxCommand publishes in results this normally doesn't make sense if you want to get an initial Result with data==null, error==null, isExecuting==false pass emitInitialCommandResult=true. emitLastResult will include the value of the last successful execution in all CommandResult events unless there is no result. By default the results Observable and the RxCommand itself behave like a PublishSubject. If you want that it acts like a BehaviourSubject, meaning every listener gets the last received value, you can set emitsLastValueToNewSubscriptions = true. initialLastResult sets the value of the lastResult property before the first item was received. This is helpful if you use lastResult as initialData of a StreamBuilder debugName optional identifier that is included when you register a globalExceptionHandler or a loggingHandler
createSync<TParam, TResult>(Func1<TParam, TResult> func, {Stream<bool>? restriction, bool emitInitialCommandResult = false, bool emitLastResult = false, bool emitsLastValueToNewSubscriptions = false, TResult? initialLastResult, String? debugName}) RxCommand<TParam, TResult>
Creates a RxCommand for a synchronous handler function with parameter that returns a value func: handler function canExecute : observable that can be used to enable/disable the command based on some other state change if omitted the command can be executed always except it's already executing isExecuting will issue a bool value on each state change. Even if you subscribe to a newly created command it will issue false For the Observable<CommandResult> that RxCommand implement this normally doesn't make sense if you want to get an initial Result with data==null, error==null, isExecuting==false pass emitInitialCommandResult=true. emitLastResult will include the value of the last successful execution in all CommandResult events unless there is no result. By default the results Observable and the RxCommand itself behave like a PublishSubject. If you want that it acts like a BehaviourSubject, meaning every listener gets the last received value, you can set emitsLastValueToNewSubscriptions = true. initialLastResult sets the value of the lastResult property before the first item was received. This is helpful if you use lastResult as initialData of a StreamBuilder debugName optional identifier that is included when you register a globalExceptionHandler or a loggingHandler
createSyncNoParam<TResult>(Func<TResult> func, {Stream<bool>? restriction, bool emitInitialCommandResult = false, bool emitLastResult = false, bool emitsLastValueToNewSubscriptions = false, TResult? initialLastResult, String? debugName}) RxCommand<void, TResult>
Creates a RxCommand for a synchronous handler function with no parameter that returns a value func: handler function canExecute : observable that can be used to enable/disable the command based on some other state change if omitted the command can be executed always except it's already executing isExecuting will issue a bool value on each state change. Even if you subscribe to a newly created command it will issue false emitLastResult will include the value of the last successful execution in all CommandResult events unless there is no result. For the Observable<CommandResult> that RxCommand publishes in results this normally doesn't make sense if you want to get an initial Result with data==null, error==null, isExecuting==false pass emitInitialCommandResult=true. By default the results Observable and the RxCommand itself behave like a PublishSubject. If you want that it acts like a BehaviourSubject, meaning every listener gets the last received value, you can set emitsLastValueToNewSubscriptions = true. initialLastResult sets the value of the lastResult property before the first item was received. This is helpful if you use lastResult as initialData of a StreamBuilder debugName optional identifier that is included when you register a globalExceptionHandler or a loggingHandler
createSyncNoParamNoResult(Action action, {Stream<bool>? restriction, bool emitInitialCommandResult = false, bool emitsLastValueToNewSubscriptions = false, String? debugName}) RxCommand<void, void>
Creates a RxCommand for a synchronous handler function with no parameter and no return type action: handler function restriction : observable that can be used to enable/disable the command based on some other state change if omitted the command can be executed always except it's already executing isExecuting will issue a bool value on each state change. Even if you subscribe to a newly created command it will issue false For the Observable<CommandResult> that RxCommand publishes in results this normally doesn't make sense if you want to get an initial Result with data==null, error==null, isExecuting==false pass emitInitialCommandResult=true. By default the results Observable and the RxCommand itself behave like a PublishSubject. If you want that it acts like a BehaviourSubject, meaning every listener gets the last received value, you can set emitsLastValueToNewSubscriptions = true. debugName optional identifier that is included when you register a globalExceptionHandler or a loggingHandler
createSyncNoResult<TParam>(Action1<TParam> action, {Stream<bool>? restriction, bool emitInitialCommandResult = false, bool emitsLastValueToNewSubscriptions = false, bool? catchAlways, String? debugName}) RxCommand<TParam, void>
Creates a RxCommand for a synchronous handler function with one parameter and no return type action: handler function canExecute : observable that can be used to enable/disable the command based on some other state change if omitted the command can be executed always except it's already executing isExecuting will issue a bool value on each state change. Even if you subscribe to a newly created command it will issue false For the Observable<CommandResult> that RxCommand publishes in results this normally doesn't make sense if you want to get an initial Result with data==null, error==null, isExecuting==false pass emitInitialCommandResult=true. By default the results Observable and the RxCommand itself behave like a PublishSubject. If you want that it acts like a BehaviourSubject, meaning every listener gets the last received value, you can set emitsLastValueToNewSubscriptions = true. debugName optional identifier that is included when you register a globalExceptionHandler or a loggingHandler