I would like to make my flutter application to be able to check the connection status and prompt a warning if the wifi and mobile network is turned off. However, I want to make this globally across the application which means the application will constantly listen for network changes until the application is exited. I do not know how to achieve that. I use Provider as the state management for my project. The package I use for checking the network status are internet_connection_checker and connectivity_plus.
main.dart
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:provider/provider.dart';
import 'package:test/page/error.dart';
import 'package:test/routes.dart';
import 'package:test/service/connection_manager.dart';
import 'package:test/service/firebase_service.dart';
import 'package:test/firebase_options.dart';
import 'package:test/globals.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
await FirebaseService().initNotifications();
await FirebaseService().initFirebaseRemoteConfig();
int mainCount = 1;
try {
await deviceManager.getCurrentLocation();
print('Main() is executed for $mainCount time(s)');
mainCount ++;
} catch(e) {
print('Error getting current location: $e');
try {
await deviceManager.getLastestLocation();
} catch (e) {
print('Error getting latest location: $e');
}
}
FlutterError.onError = (FlutterErrorDetails details) {
runApp(const MaterialApp(
debugShowCheckedModeBanner: false,
home: AppErrorScreen(),
));
};
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => ConnectionManager()),
ChangeNotifierProvider(create: (_) => Globals()),
],
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: AppRoute().route,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
debugShowCheckedModeBanner: false,
);
}
}
connection.dart
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
class ConnectionManager with ChangeNotifier {
final Connectivity _connectivity = Connectivity();
bool _isConnected = false;
late StreamSubscription<List<ConnectivityResult>> _subscription;
bool get isConnected => _isConnected;
ConnectionManager() {
_subscription = _connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
_checkInitialConnection();
}
Future<void> _checkInitialConnection() async {
final List<ConnectivityResult> connectivityResult = await _connectivity.checkConnectivity();
// await _updateConnectionStatus(connectivityResult);
}
Future<void> _updateConnectionStatus(List<ConnectivityResult> result) async {
bool isConnected = false;
if (result != ConnectivityResult.none) {
try {
final lookupResult = await InternetAddress.lookup('google.com');
isConnected = lookupResult.isNotEmpty;
} on SocketException catch (_) {
isConnected = false;
}
}
_isConnected = isConnected;
notifyListeners();
}
I have tried to make the connection check function into a stream however I am new to stream I am not quite sure how can I implement it across the application.