CheckInternet<St> mixin
This mixin can be used to check if there is internet when you run some action that needs
internet connection. Just add with CheckInternet
to your action. For example:
class LoadText extends ReduxAction<AppState> with CheckInternet {
Future<String> reduce() async {
var response = await http.get('http://numbersapi.com/42');
return response.body;
}}
I will automatically check if there is internet before running the action. If there is no internet, the action will fail, stop executing, and will show a dialog to the user with title: 'There is no Internet' and content: 'Please, verify your connection.'.
Also, you can display some information in your widgets when the action fails:
if (context.isFailed(LoadText)) Text('No Internet connection');
Or you can use the exception text itself:
if (context.isFailed(LoadText)) Text(context.exceptionFor(LoadText)?.errorText ?? 'No Internet connection');
If you don't want the dialog to open, you can add the NoDialog mixin.
If you want to customize the dialog or the errorText
, you can override the
method connectionException and return an UserException with the desired message.
IMPORTANT: It only checks if the internet is on or off on the device, not if the internet provider is really providing the service or if the server is available. So, it is possible that this function returns true and the request still fails.
Notes:
- This mixin can safely be combined with NonReentrant.
- It should not be combined with other mixins that override before.
- It should not be combined with other mixins that check the internet connection.
See also:
- NoDialog - To just show a message in your widget, and not open a dialog.
- AbortWhenNoInternet - If you want to silently abort the action when there is no internet.
- Superclass Constraints
- ReduxAction<
St>
- ReduxAction<
Properties
-
dispatch
→ Dispatch<
St> -
Dispatches the action, applying its reducer, and possibly changing the store state.
The action may be sync or async.
no setterinherited
-
dispatchAll
→ List<
ReduxAction< Function(List<St> >ReduxAction< actions, {bool notify})St> > -
Dispatches all given
actions
in parallel, applying their reducer, and possibly changing the store state. It returns the same list ofactions
, so that you can instantiate them inline, but still get a list of them.no setterinherited -
dispatchAndWait
→ DispatchAndWait<
St> -
Dispatches the action, applying its reducer, and possibly changing the store state.
The action may be sync or async. In both cases, it returns a Future that resolves when
the action finishes.
no setterinherited
-
dispatchAndWaitAll
→ Future<
List< Function(List<ReduxAction< >St> >ReduxAction< actions, {bool notify})St> > -
Dispatches all given
actions
in parallel, applying their reducers, and possibly changing the store state. The actions may be sync or async. It returns a Future that resolves when ALL actions finish.no setterinherited -
dispatchAsync
→ DispatchAsync<
St> -
no setterinherited
-
dispatchSync
→ DispatchSync<
St> -
Dispatches the action, applying its reducer, and possibly changing the store state.
However, if the action is ASYNC, it will throw a StoreException.
no setterinherited
- env → Object?
-
Gets the store environment.
This can be used to create a global value, but scoped to the store.
For example, you could have a service locator, here, or a configuration value.
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- ifOpenDialog → bool
-
no setter
- initialState → St
-
Returns the state as it was when the action was dispatched.
no setterinherited
- internetOnOffSimulation → bool?
-
If you are running tests, you can override this method to simulate the internet connection
as permanently on or off.
Return
true
if there is internet, andfalse
if there is no internet. Returnnull
to use the real internet connection status.no setter - isFinished → bool
-
Returns true only if the action finished with no errors.
In other words, if the methods before, reduce and after all finished executing
without throwing any errors.
no setterinherited
- microtask → Future
-
To wait for the next microtask:
await microtask;
no setterinherited - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state → St
-
no setterinherited
- stateTimestamp → DateTime
-
no setterinherited
- status → ActionStatus
-
no setterinherited
-
store
→ Store<
St> -
no setterinherited
Methods
-
abortDispatch(
) → bool -
If abortDispatch returns true, the action will NOT be dispatched:
before
,reduce
andafter
will not be called, and the action will not be visible to the store observers.inherited -
after(
) → void -
This is an optional method that may be overridden to run during action
dispatching, after
reduce
. If this method throws an error, the error will be swallowed (will not throw). So you should only run code that can't throw errors. It may be synchronous only. Note this method will always be called, even if errors were thrown bybefore
orreduce
.inherited -
assertUncompletedFuture(
) → void -
An async reducer (one that returns Future<AppState?>) must never complete without at least
one await, because this may result in state changes being lost. It's up to you to make sure
all code paths in the reducer pass through at least one
await
.inherited -
before(
) → Future< void> -
This is an optional method that may be overridden to run during action
dispatching, before
reduce
. If this method throws an error, thereduce
method will NOT run, but the methodafter
will. It may be synchronous (returningvoid
) ou async (returningFuture<void>
). You should NOT returnFutureOr
.override -
checkConnectivity(
) → Future< ConnectivityResult> -
clearExceptionFor(
Object actionTypeOrList) → void -
Removes the given
actionTypeOrList
from the list of action types that failed.inherited -
connectionException(
ConnectivityResult result) → UserException -
exceptionFor(
Object actionTypeOrList) → UserException? -
Returns the UserException of the
actionTypeOrList
that failed.inherited -
isFailed(
Object actionOrTypeOrList) → bool -
Returns true if an
actionOrActionTypeOrList
failed with an UserException. Note: This method uses the EXACT type inactionOrActionTypeOrList
. Subtypes are not considered.inherited -
isSync(
) → bool -
Returns true if the action is SYNC, and false if the action is ASYNC.
The action is considered SYNC if the
before
method, thereduce
method, and thewrapReduce
methods are all synchronous.inherited -
isWaiting(
Object actionOrTypeOrList) → bool -
You can use isWaiting to check if:
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
prop<
V> (Object? key) → V -
Gets a property from the store.
This can be used to save global values, but scoped to the store.
For example, you could save timers, streams or futures used by actions.
inherited
-
reduce(
) → FutureOr< St?> -
The
reduce
method is the action reducer. It may read the action state, the store state, and then return a new state (ornull
if no state change is necessary).inherited -
runtimeTypeString(
) → String -
Returns the runtimeType, without the generic part.
inherited
-
setProp(
Object? key, Object? value) → void -
Sets a property in the store.
This can be used to save global values, but scoped to the store.
For example, you could save timers, streams or futures used by actions.
inherited
-
setStore(
Store< St> store) → void -
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
waitAllActions(
List< ReduxAction< actions, {bool completeImmediately = false}) → Future<St> >void> -
Returns a future that completes when ALL given
actions
finished dispatching. You MUST provide at list one action, or an error will be thrown.inherited -
waitCondition(
bool condition(St), {int? timeoutMillis}) → Future< ReduxAction< St> ?> -
Returns a future which will complete when the given state
condition
is true. If the condition is already true when the method is called, the future completes immediately.inherited -
wrapError(
Object error, StackTrace stackTrace) → Object? -
If any error is thrown by
reduce
orbefore
, you have the chance to further process it by usingwrapError
. Usually this is used to wrap the error inside of another that better describes the failed action. For example, if some action converts a String into a number, then instead of throwing a FormatException you could do:inherited -
wrapReduce(
Reducer< St> reduce) → Reducer<St> -
You may wrap the reducer to allow for some pre- or post-processing.
For example, if you want to prevent an async reducer to change the current state,
if the current state has already changed since when the reducer started:
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- forceInternetOnOffSimulation ↔ bool? Function()
-
If you have a configuration object that specifies if the internet connection should be
simulated as on or off, you can replace this method to return that configuration value.
For example:
CheckInternet.forceInternetOnOffSimulation = () => Config.isInternetOn;
Returntrue
if there is internet, andfalse
if there is no internet. Returnnull
to use the real internet connection status.getter/setter pair