Flutter Future<bool> vs bool type

You need to get the bool out of Future<bool>. Use can then block or await.

with then block

_checkConnection() {
  Utiliy.checkConnection().then((connectionResult) {
    Utility.showAlert(context, connectionResult ? "OK": "internet needed");
  })
}

with await

_checkConnection() async {
 bool connectionResult = await Utiliy.checkConnection();
 Utility.showAlert(context, connectionResult ? "OK": "internet needed");
}

For more details, refer here.


In Future functions you must return future results, so you need to change the return of:

return true;

To:

return Future<bool>.value(true);

So full function with correct return is:

 static Future<bool> checkConnection() async{

    ConnectivityResult connectivityResult = await (new Connectivity().checkConnectivity());

    debugPrint(connectivityResult.toString());

    if ((connectivityResult == ConnectivityResult.mobile) || (connectivityResult == ConnectivityResult.wifi)){
      return Future<bool>.value(true);
    } else {
      return Future<bool>.value(false);
    }
  }

Tags:

Future

Flutter