-1

i created flutter app that shows a taost message when no internet connection, but i was trying to return a page clarifies that no internet connection ( before entering the app) instead of toast message is there a way to do it?

class _NotificationState extends State<Notification> {
  var subscription;
  var connectionStatus;

  @override
  void initState() {
subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {         
        setState(() => connectionStatus = result);
        checkInternetConnectivity();
});
    
    checkInternetConnectivity();
    // checkLoginStatus();
    
    super.initState();
  }

  checkInternetConnectivity() {
   if (connectionStatus == ConnectivityResult.none) {
      return 
 
      Fluttertoast.showToast(
         msg: "Check your internet connection",
           toastLength: Toast.LENGTH_LONG,
           gravity: ToastGravity.TOP,
          timeInSecForIosWeb: 10,
           backgroundColor: Colors.red,
          textColor: Colors.white,
           fontSize: 16.0
       );
     }
  }
//PS: i tried to return Internet() page instead but it's not working
  @override
  dispose() {
    super.dispose();
    subscription.cancel();
  }
  

  


  Widget build(BuildContext context) {
raya
  • 113
  • 1
  • 1
  • 10
  • 1
    Returning a widget from your method will never work. You need to push a route i.e, your `Internet` page to make it work. – Ravi Singh Lodhi Nov 20 '20 at 03:35
  • As mentioned by @RaviSinghLodhi, yes you need to push a route to the page that you wanted and the only return your toast message inside your Internet page – HeIsDying Nov 20 '20 at 03:39

2 Answers2

0

Your initState method contains duplicate method calls to checkInternetConnectivity. Remove one of them:

@override
  void initState() {
subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {         
        setState(() => connectionStatus = result);
        checkInternetConnectivity();
});
    
    // checkInternetConnectivity(); <--- Remove this
    // checkLoginStatus();
    
    super.initState();
  }

Finally, your method should look something like this:

void checkInternetConnectivity() {
   if (connectionStatus == ConnectivityResult.none) {
     Navigator.of(context).push(MaterialPageRoute(
       buider: (BuildContext context) => Internet(),
     )); 
   }
}
Ravi Singh Lodhi
  • 2,605
  • 1
  • 9
  • 12
  • itried your answer and turned wifi off but app still works normal and didn't navigate me to internet page :\ – raya Nov 20 '20 at 09:16
  • it throws " The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget." – raya Nov 20 '20 at 09:26
  • Okay, then use `Navigator.of(context).push`. I have updated the answer. Please check. – Ravi Singh Lodhi Nov 21 '20 at 06:30
  • Still same :/ it opens app normally and wifi is turned off, but it works good if returned toast instead of internet page – raya Nov 21 '20 at 10:50
0

Two common ways of navigating from one page to another

  1. Without the use of route
  Navigator.push(
        context,
        MaterialPageRoute(
        builder: (context) => InternetPage(
            // you can pass parameter here to the next page
            // remember to create instance of dataToNextPage at the page you are navigating to
            dataToNextPage: dataFromCurrentPage,
        ),
 ));
  1. With the use of named Route. You need to specify the route you will be using link for more info
Navigator.pushNamed(context, '/internetPage', arguments: {});
HeIsDying
  • 335
  • 5
  • 16
  • itried your answer and turned wifi off but app still works normal and didn't navigate me to internet page :\ – raya Nov 20 '20 at 09:16
  • try to debug it. Did you try to debug where is the last session before your program stuck? Did your `if (connectionStatus == ConnectivityResult.none) ` executed? – HeIsDying Nov 20 '20 at 11:25
  • it throws " The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget." – raya Nov 20 '20 at 11:37
  • @raya https://stackoverflow.com/questions/50124355/flutter-navigator-not-working read this to solve the error – HeIsDying Nov 20 '20 at 12:12