Keyboard Shortcuts

j / kScroll down / up
ggScroll to top
GScroll to bottom
K / /Open search
?Show this help
EscClose search / help
n / NNext / previous section
hGo to landing
dGo to docs
:Command mode — type section name

Press ? or Esc to close

Systems

Connectivity

Connectivity is a single ConnectivityCubit plus a reactive mixin that lets any list auto-reload when the network returns.

The cubit

Listens to connectivity_plus and exposes isOffline. It keeps a Set of reload listeners and fires them on the offline→online transition only.

lib/config/network/connectivity_cubit/connectivity_cubit.dart
class ConnectivityCubit extends Cubit<ConnectivityState> {
  final _listeners = <ConnectivityReloadMixin>{};

  void register(ConnectivityReloadMixin l) => _listeners.add(l);
  void unregister(ConnectivityReloadMixin l) => _listeners.remove(l);

  void _onStatusChanged(ConnectivityResult r) {
    final offline = r == ConnectivityResult.none;
    if (!offline && state.wasOffline) {
      for (final l in _listeners) l.onReconnected();
    }
    emit(state.copyWith(isOffline: offline));
  }
}

The mixin

List screens mix in ConnectivityReloadMixin and register themselves. onReconnected refreshes the list.

lib/config/network/connectivity_cubit/connectivity_reload_mixin.dart
mixin ConnectivityReloadMixin {
  void onReconnected();
}

// In a paginated cubit:
@override
void onReconnected() => refresh();

Offline banner

ConnectivityWrapper slides a banner down from the top whenever isOffline is true. It sits above the shell, below toasts.

lib/config/ui/connectivity_wrapper.dart
BlocBuilder<ConnectivityCubit, ConnectivityState>(
  builder: (context, state) => AnimatedSlide(
    offset: state.isOffline ? Offset.zero : const Offset(0, -1),
    child: const _OfflineBanner(),
  ),
)
sam's arch — A Pragmatic Flutter Architecture