0

I am trying to check the internet connection of the mobile device. I am using below code to check the connectivity.

import 'package:flutter/material.dart';
import 'package:internet_connection_checker/internet_connection_checker.dart';

class RedirectPage extends StatelessWidget {
  final int? status;
  
  @override
  Widget build(BuildContext context) {
      bool? isDeviceConnected;
      
      () async {
        print("a");
        print(123);
        isDeviceConnected = await checkConnection();
        print(888);
      };
      
      if (isDeviceConnected != null && isDeviceConnected == false) {
        return AppNetworkConnectivityHome();
      } else{
        return HomePage();      
      }
}
}



print(isDeviceConnected); //giving null for the first time and true or false on the second time.

Future<bool?> checkConnection() async {
  bool a = false;
  a = await InternetConnectionChecker().hasConnection;
  print(a);
  return a;
}

how to force wait for the await function to complete

5
  • could you add you full code? where do you call that print? Commented Nov 14, 2022 at 13:04
  • Next line where i am calling the await Commented Nov 14, 2022 at 13:26
  • could you update your code and add your full class code so we can run it? Commented Nov 14, 2022 at 13:30
  • @eamirho3ein code updated. Commented Nov 14, 2022 at 13:36
  • You can't call code like that in the build method. The build method can be run at least 60 times per second.. Commented Nov 14, 2022 at 13:56

2 Answers 2

2

You'd have to await the method call. You've currently defined it as an anonymous function, so depending on where and how you execute it there will be some differences. But it will work if you instead do something like this:

Future<bool?> myMethod() async {
   return await InternetConnectionChecker().hasConnection;
}

...
print(await myMethod());
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. It makes no differnce. still doing the same.
1

You can't call async function in build method, you need to use FutureBuilder like this:

return FutureBuilder<bool>(
        future: checkConnection(),
        builder: (context, snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
              return Text('Loading....');
            default:
              if (snapshot.hasError) {
                return Text('Error: ${snapshot.error}');
              } else {
                bool data = snapshot.data ?? true;

                if (!data) {
                    return AppNetworkConnectivityHome();
                } else{
                    return HomePage();      
                }
              }
          }
        },
      )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.