Agniswar Chakraborty
Agniswar Chakraborty

Reputation: 287

Check Internet Connectivity after splash

I am a newbie to flutter. I am developing an app that contains a splash screen widget then the home screen will appear. I just want to check if there is any internet connection or not, if yes then it will go to the home screen otherwise it will close. I have checked the internet connection programmatically. It's ok, but without any internet connection, it goes to the home screen. Please help in this matter. Thanks in advance.

class Splashscreen extends StatefulWidget {

          @override
           State<StatefulWidget> createState() =>  _Splashscreenmain();
            }

         class _Splashscreenmain extends State<Splashscreen>{

  Helperfunction helperfunction = new Helperfunction();
  @override
  void initState() {
  super.initState();
  startSplashScreen();
  }

  startSplashScreen() async {
    var duration = const Duration(seconds: 10);
    if (helperfunction.internetConnection() != "No Internet"){
    return Timer(duration, () {

        Navigator.of(context).pushReplacement(
            new MaterialPageRoute(builder: (BuildContext context) {
              return MyApp();
            }));
    });
  }
    else {

      print(helperfunction.internetConnection());
      SystemChannels.platform.invokeMethod('SystemNavigator.pop');
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Container(
 child: Image.asset('assets/images/splashscreen.png',fit: BoxFit.cover,
   height: double.infinity,
   width: double.infinity,
   alignment: Alignment.center,),

      ),

    );
  }



} 

I have printed internetConnection() function result but the 'else' part is not executed.

Upvotes: 0

Views: 728

Answers (1)

griffins
griffins

Reputation: 8264

You want to close app if no internet connection. You can do this with SystemNavigator.pop(). does not work for ios as in IOS an app should not exit itself.I would suggest you show the user a dialog to enable internet connectivty.

SystemChannels.platform.invokeMethod('SystemNavigator.pop');

from docs . Instructs the system navigator to remove this activity from the stack and return to the previous activity.

On iOS, calls to this method are ignored because Apple's human interface guidelines state that applications should not exit themselves.

This method should be preferred over calling dart:io's exit method, as the latter may cause the underlying platform to act as if the application had crashed.

Upvotes: 1

Related Questions