0
  • i make an enum for a connectivity :
enum ConnectivityStatus{
  Wifi,
  Cellular,
  Offline
}
  • then i create a service to check the Connectivity :
import 'dart:async';

import 'package:Zabatnee/activities_app/enum/connectivity_status.dart';
import 'package:connectivity/connectivity.dart';

class ConnectivityService{

StreamController<ConnectivityStatus> connectionStatusController = StreamController<ConnectivityStatus>();

ConnectivityService(){
  Connectivity().onConnectivityChanged.listen((ConnectivityResult result) { 
    var connectionStatus = _getStatusFromResult(result);
    connectionStatusController.add(connectionStatus);
  });
}
  ConnectivityStatus _getStatusFromResult(ConnectivityResult result) {
    switch (result) {
      case ConnectivityResult.mobile:
        return ConnectivityStatus.Cellular;
      case ConnectivityResult.wifi:
        return ConnectivityStatus.Wifi;
      case ConnectivityResult.none:
        return ConnectivityStatus.Offline;
      default:
        return ConnectivityStatus.Offline;
    }
  }
}
  • and what i need is to check the connectivity status in all screen that i created. Can I do it once for all screens all, or i must check the connnectivity for each one individually.
Ahmad Mohy
  • 391
  • 2
  • 6
  • 18

1 Answers1

2

You can check the connectivity status once & that will be implemented for all of your app.

You just have to create a wrapper class & subscribe to the Connectivity stream & apply the logic in this wrapper class.

Your whole widget will be wrapped by this widget.

MaterialApp(
  ..
  home: ConnectivityWrapper(
    childWidget: YourWidget(), // replace this with your own home widget
  ),
);

The wrapper widget will look something like this:

class ConnectivityWrapper extends StatefulWidget {
  
  ConnectivityWrapper(this.childWidget);
  
  final Widget childWidget;

  @override
  _ConnectivityWrapperState createState() => _ConnectivityWrapperState();
}

class _ConnectivityWrapperState extends State<ConnectivityWrapper> {

  StreamSubscription<ConnectivityStatus> subscription;

  @override
  void initState() {
    super.initState();

    subscription = connectionStatusController.stream.listen((status) 
      {
        if(status == ConnectivityStatus.Offline) {
          // Your logic here (Toast message or something else)
        }
      }, 
      onDone() {
        // Your logic here
      }, 
      onError: () {
        // Your logic here
      });
  }

  @override
  Widget build(BuildContext context) {
    return widget.childWidget;
  }

  @override
  void dispose() {
    // unsubscribe to the stream
    subscription.cancel();
    super.dispose();
  }
}
Ravi Singh Lodhi
  • 2,605
  • 1
  • 9
  • 12
  • thanks, it works when i use something like print, but when i try to use a snackbar [ Scaffold.of(context).showSnackBar(]...... i got this error [Scaffold.of() called with a context that does not contain a Scaffold. – Ahmad Mohy Nov 01 '20 at 10:34
  • This will be helpful for you: https://stackoverflow.com/questions/57267165/how-to-show-snackbar-without-scaffold – Ravi Singh Lodhi Nov 01 '20 at 10:37
  • subscription = connectionStatusController.stream.listen((status) in this line i not have the variable connectionStatusController, to listen to the connection status how can i got it to listen to this – Ahmad Mohy Nov 01 '20 at 13:15
  • 1
    You can try to use the same variable that you have created in your `ConnectivityService` class. – Ravi Singh Lodhi Nov 01 '20 at 14:40