initialize method
Performs an initial connectivity check and starts the merged event stream.
Calling this more than once is safe — subsequent calls are no-ops. Must be called before the cubit's state is relied upon by the UI.
Implementation
Future<void> initialize() async {
if (_initialized) return;
_initialized = true;
await _checkOnce();
final StreamSubscription<dynamic> sub = Rx.merge(<Stream<dynamic>>[
connectivity.onConnectivityChanged,
internetConnection.onStatusChange,
]).debounceTime(const Duration(milliseconds: 300)).listen((_) => _checkOnce());
// If close() was called while the awaits above were in progress, cancel
// immediately rather than storing a subscription that will never be cleaned up.
if (_closed) {
await sub.cancel();
} else {
_mergedSubscription = sub;
}
}