NetworkManager class

The primary API surface of the plugin.

final manager = NetworkManager();

manager.stateStream.listen((state) {
  switch (state) {
    case Loading()  => showSpinner();
    case Offline()  => showBanner();
    case Syncing()  => showSyncProgress(state.progress);
    case Success()  => showData(state.data);
    case Error()    => showError(state.message);
    case Idle()     => hideIndicators();
  }
});

final user = await manager.request(
  () => api.getUser(),
  cacheKey: 'user',
  strategy: CacheFirst(),
);
Available extensions

Constructors

NetworkManager({NetworkManagerConfig? config, NetworkStatusMonitor? monitor, CacheManager? cacheManager, RequestQueue? queue, SyncEngine? syncEngine, Connectivity? connectivity, NetworkLogger? logger})
Creates a NetworkManager with sensible defaults.

Properties

cache CacheManager
The cache manager.
no setter
connectivityStatus ConnectivityStatus
Current connectivity status.
no setter
hashCode int
The hash code for this object.
no setterinherited
isOnline bool
Whether the device is currently online.
no setter
monitor NetworkStatusMonitor
The connectivity monitor.
no setter
queue RequestQueue
The offline request queue.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stateStream Stream<NetworkState>
Unified stream of NetworkState changes.
no setter
syncEngine SyncEngine
The sync engine.
no setter

Methods

bindToEmit(void emit(NetworkState)) StreamSubscription<NetworkState>

Available on NetworkManager, provided by the NetworkManagerBlocExtension extension

Creates a StreamSubscription that maps NetworkState events into Bloc-compatible emit calls.
clearCache() → void
Clear all cached data.
clearQueue() → void
Clear the request queue.
createNotifier() NetworkStateNotifier

Available on NetworkManager, provided by the NetworkManagerRiverpodExtension extension

Creates a NetworkStateNotifier bound to this manager.
dispose() Future<void>
Dispose all resources. The manager must not be used after this.
executeAndEmit<T>(Future<T> requestFn(), {required void emit(NetworkState), String? cacheKey, NetworkStrategy? strategy, Duration? cacheTtl}) Future<T?>

Available on NetworkManager, provided by the NetworkManagerBlocExtension extension

Execute a request and pipe NetworkState changes into emit.
initialize() Future<void>
Initialize the manager and start listening for connectivity.
invalidateCache(String key) → void
Invalidate a cache entry.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
request<T>(Future<T> requestFn(), {String? cacheKey, NetworkStrategy? strategy, Duration? cacheTtl, RetryPolicy? retryPolicy, bool queueIfOffline = true}) Future<T>
Execute a network request with strategy-based resolution.
syncNow() Future<List<QueueProcessResult>>
Manually trigger sync of queued requests.
toString() String
A string representation of this object.
inherited

Operators

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