RxCommand<TParam, TResult> class

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
Available Extensions

Constructors

RxCommand(Subject<TResult> _resultsSubject, Stream<bool> canExecuteRestriction, bool _emitLastResult, bool _resultSubjectIsBehaviourSubject, TResult lastResult)

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
read-only
first Future<TResult>
The first element of this stream. [...]
read-only, inherited
hashCode → int
The hash code for this object. [...]
read-only, inherited
isBroadcast → bool
Whether this stream is a broadcast stream.
read-only, inherited
isEmpty Future<bool>
Whether this stream contains any elements. [...]
read-only, inherited
isExecuting Stream<bool>
Observable stream that issues a bool on any execution state change of the command
read-only
last Future<TResult>
The last element of this stream. [...]
read-only, inherited
lastResult ↔ TResult
The result of the last successful call to execute. This is especially handy to use as initialData of Flutter StreamBuilder
read / write
length Future<int>
The number of elements in this stream. [...]
read-only, inherited
next Future<TResult>
This property is a utility which allows us to chain RxCommands together.
read-only
results Stream<CommandResult<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.
read-only
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited
single Future<TResult>
The single element of this stream. [...]
read-only, inherited
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.
read / write
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
read-only

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
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 non-existent 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
Returns 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 Methods

createAsync<TParam, TResult>(AsyncFunc1<TParam, TResult> func, {Stream<bool> canExecute, bool emitInitialCommandResult: false, bool emitLastResult: false, bool emitsLastValueToNewSubscriptions: false, TResult initialLastResult}) 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
createAsyncNoParam<TResult>(AsyncFunc<TResult> func, {Stream<bool> canExecute, bool emitInitialCommandResult: false, bool emitLastResult: false, bool emitsLastValueToNewSubscriptions: false, TResult initialLastResult}) 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
createAsyncNoParamNoResult(AsyncAction action, {Stream<bool> canExecute, bool emitInitialCommandResult: false, bool emitsLastValueToNewSubscriptions: false}) 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.
createAsyncNoResult<TParam>(AsyncAction1<TParam> action, {Stream<bool> canExecute, bool emitInitialCommandResult: false, bool emitsLastValueToNewSubscriptions: false}) 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.
createFromStream<TParam, TResult>(StreamProvider<TParam, TResult> provider, {Stream<bool> canExecute, bool emitInitialCommandResult: false, bool emitLastResult: false, bool emitsLastValueToNewSubscriptions: false, TResult initialLastResult}) 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 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
createSync<TParam, TResult>(Func1<TParam, TResult> func, {Stream<bool> canExecute, bool emitInitialCommandResult: false, bool emitLastResult: false, bool emitsLastValueToNewSubscriptions: false, TResult initialLastResult}) 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
createSyncNoParam<TResult>(Func<TResult> func, {Stream<bool> canExecute, bool emitInitialCommandResult: false, bool emitLastResult: false, bool emitsLastValueToNewSubscriptions: false, TResult initialLastResult}) 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
createSyncNoParamNoResult(Action action, {Stream<bool> canExecute, bool emitInitialCommandResult: false, bool emitsLastValueToNewSubscriptions: false}) RxCommand<void, void>
Creates a RxCommand for a synchronous 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 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.
createSyncNoResult<TParam>(Action1<TParam> action, {Stream<bool> canExecute, bool emitInitialCommandResult: false, bool emitsLastValueToNewSubscriptions: false}) 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.