boolean return a null - flutter

110

You cannot. You want to start an asynchronous method, not wait for it's result and still return that result you never cared to wait for. You have to wait for the result, there is no other option.

I don't know what you want to do, but what you need to do is this:

Future<bool> isConnectedToInternet() async {
  var internet = await check();
  return (internet != null && internet);
}

If that does not work out with your method declaration you want to override, maybe you need to change that.

Share:
110
Arpit todewale
Author by

Arpit todewale

Updated on December 21, 2022

Comments

  • Arpit todewale
    Arpit todewale over 1 year

    I am new to flutter and want to return a boolean in the following method

    @override
    bool isConnectedToInternet() {
    bool network;
    check().then((internet) {
     if (internet != null && internet) {
      network = true;
      print("connected");
     }else {
      network = false;
      print("not connected");
     }
    });
    print(network.toString());
    return network;
    

    }

    when I run the code, the print is working but the final return gives me null. how to solve this thing.

  • Arpit todewale
    Arpit todewale almost 4 years
    i was returning a bool internet wheres i needed to return Future<bool>internet. modified the code and it worked like a charm.