skip to Main Content

Trying to check if a user is connected to the internet before i redirect them to the homepage. connectivity result shows as connectivity.wifi when am connected to the internet and returns true. but when i disconnect the internet on the emmulator it shows connectivity.none and returns true which still redirect the user to the homepage. i don’t want to direct the user to the homepage.


class SplashScreen extends StatefulWidget {
  const SplashScreen({super.key});

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {

  Future<bool> checkInternetConnectivity() async {
    try {
      final connectivityResult = await Connectivity().checkConnectivity();
      print('Connectivity result: $connectivityResult'); 
      return connectivityResult != ConnectivityResult.none;
    } catch (e) {
      print('Error checking connectivity: $e'); 
      return false; // Assuming no connectivity in case of an error
    }
  }


this is the redirect code

void startTimer() {
    Timer(Duration(seconds: 5), () async {
      bool isConnected = await checkInternetConnectivity();
      print('isConnected: $isConnected'); // Debug print

      if (isConnected) {
        if (await firebaseAuth.currentUser != null) {
          firebaseAuth.currentUser != null
              ? AssistanceMethods.readCurrentUserOnlineInfo()
              : null;
          Navigator.pushReplacement(
              context, MaterialPageRoute(builder: (c) => MainScreen()));
        } else {
          Navigator.pushReplacement(
              context, MaterialPageRoute(builder: (c) => LoginScreen()));
        }
      } else {
        Navigator.pushReplacement(
            context, MaterialPageRoute(builder: (c) => ErrorMsgScreen()));
      }
    });
  }

2

Answers


  1. To ensure the user is redirected to an error screen if there’s no internet connection, modify your checkInternetConnectivity method to correctly handle the connectivity status:

    import 'package:connectivity_plus/connectivity_plus.dart';
    import 'package:flutter/material.dart';
    import 'dart:async';
    
    class SplashScreen extends StatefulWidget {
      const SplashScreen({super.key});
    
      @override
      State<SplashScreen> createState() => _SplashScreenState();
    }
    
    class _SplashScreenState extends State<SplashScreen> {
      Future<bool> checkInternetConnectivity() async {
        try {
          final connectivityResult = await Connectivity().checkConnectivity();
          return connectivityResult != ConnectivityResult.none;
        } catch (e) {
          return false; // Assuming no connectivity in case of an error
        }
      }
    
      void startTimer() {
        Timer(Duration(seconds: 5), () async {
          bool isConnected = await checkInternetConnectivity();
          if (isConnected) {
            Navigator.pushReplacement(context, MaterialPageRoute(builder: (c) => HomePage()));
          } else {
            Navigator.pushReplacement(context, MaterialPageRoute(builder: (c) => ErrorScreen()));
          }
        });
      }
    
      @override
      void initState() {
        super.initState();
        startTimer();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(body: Center(child: CircularProgressIndicator()));
      }
    }
    
    class HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(body: Center(child: Text('Home Page')));
      }
    }
    
    class ErrorScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(body: Center(child: Text('Error: No Internet Connection')));
      }
    }
    

    Make sure you have the connectivity_plus package added in your pubspec.yaml

    Login or Signup to reply.
  2. As per the documentation here, the connectivityResult is a list and it should be used like this:

    final List<ConnectivityResult> connectivityResult = await (Connectivity().checkConnectivity());
    final noInternet = connectivityResult.contains(ConnectivityResult.none)
    

    So your checkInternetConnnectivity() method should be:

    Future<bool> checkInternetConnectivity() async {
        try {
          final connectivityResult = await Connectivity().checkConnectivity();
          print('Connectivity result: $connectivityResult'); 
          return !connectivityResul.contains(ConnectivityResult.none);
        } catch (e) {
          print('Error checking connectivity: $e'); 
          return false; // Assuming no connectivity in case of an error
        }
      }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search