import 'dart:async'; import 'package:connectivity/connectivity.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:teso/util/consts.dart'; import 'package:http/http.dart' as http; class MyConnectivity { MyConnectivity._internal(); static final MyConnectivity _instance = MyConnectivity._internal(); static MyConnectivity get instance => _instance; Connectivity connectivity = Connectivity(); StreamController controller = StreamController.broadcast(); Stream get myStream => controller.stream; void initialise() async { ConnectivityResult result = await connectivity.checkConnectivity(); _checkStatus(result); connectivity.onConnectivityChanged.listen((result) { _checkStatus(result); }); } void _checkStatus(ConnectivityResult result) async { bool isOnline = false; try { SharedPreferences prefs = await SharedPreferences.getInstance(); Map requestHeaders = { // 'Content-type': 'application/json', 'Authorization': prefs.getString("tokensTeso") }; var register2 = serverLocation + 'serverstatus'; var client1 = await http .get(Uri.parse(register2), headers: requestHeaders) .timeout(Duration(seconds: 10)); if (client1.statusCode == 200) { await prefs.setString("tokensTeso", "Bearer " + client1.body); isOnline = true; } } catch (e) { isOnline = false; } controller.sink.add({result: isOnline}); } void disposeStream() => controller.close(); }