'check internet connection not remove pop-up flutter
I'm trying to insert a listener in my app that always checks the connection, both when I'm with the app open and when it's in the background. with transition from wifi to 3g too.
I am using the connectivity_plus library and I am declaring a listener in the iniot and in the dispose I delete the pop-up, I do not understand why I still have cases in which the pop-up does not work well. for example, if I am with wifi and 3G active, I go to the background I deactivate the wifi, if I enter the app it gives me the pop-up that I am not connected to the internet.
I don't understand where I'm wrong
class ConnectivityStatus {
factory ConnectivityStatus() => _instance;
ConnectivityStatus._() {
_init();
}
static final ConnectivityStatus _instance = ConnectivityStatus._();
FlashController? _flashController;
StreamSubscription? _lastSub;
void _checkInternetConnectivity(ConnectivityResult status) {
Future.delayed(const Duration(milliseconds: 50), () {
if ([
ConnectivityResult.mobile,
ConnectivityResult.wifi,
].contains(status)) {
// I am connected to a mobile network.
if (_flashController != null) _flashController!.dismiss();
_flashController = null;
} else {
logger.w('[Provider-checkInternetConnectivity] Missing connectivity');
showFlash(
context: Get.context!,
onWillPop: () async => false,
persistent: true,
builder: (context, controller) {
_flashController = controller;
return Flash(
controller: controller,
position: FlashPosition.top,
behavior: FlashBehavior.fixed,
useSafeArea: true,
backgroundColor: AppColors().red,
barrierDismissible: false,
enableVerticalDrag: false,
child: FlashBar(
content: localizations.checkInternet.rich(
style: const TextStyle(color: Colors.white),
),
icon: Padding(
padding: const EdgeInsets.symmetric(horizontal: 15),
child: SvgPicture.asset(
'assets/svg/noWifi_white.svg',
height: 24,
width: 24,
),
),
),
);
},
);
}
});
}
void _onStatusChange(AppLifecycleState state) {
logger.i('[Connectivity-status] State changed: $state');
if (state == AppLifecycleState.inactive ||
state == AppLifecycleState.paused ||
state == AppLifecycleState.resumed ||
state == AppLifecycleState.detached) {
_lastSub?.cancel();
_lastSub = Connectivity()
.onConnectivityChanged
.listen(_checkInternetConnectivity);
}
}
void _init() {
if (_flashController != null) _flashController?.dismiss();
StreamGroup.merge([
Future.value(AppLifecycleState.resumed).asStream(),
AppStatus().lifecycleStream
]).listen(_onStatusChange);
}
@protected
void dispose() {
if (_lastSub != null) _lastSub?.cancel();
if (_flashController != null) _flashController?.dismiss();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
