Search code examples
flutterdartflutter-layout

Flutter/Dart convert future bool to bool


Can some one help me to identify the issue in below piece of code

void main() async {
  bool c =getstatus();
  print(c);
  }

Future<bool> getMockData() {
  return Future.value(false);
}

bool getstatus() async   
{
  Future<bool> stringFuture = getMockData();
  bool message =  stringFuture;
  return(message); // will print one on console.

}

Solution

  • To get values from a Future(async) method, you have to await them. And after await the variable you get is not a Future anymore. So basically your code should look like this:

    void main() async {
      bool c = await getstatus();
      print(c);
    }
    
    Future<bool> getMockData() {
      return Future.value(false);
    }
    
    Future<bool> getstatus() async {
      bool message = await getMockData();
      return message;
    }