Home > database >  How can I view the return value from Future<bool>
How can I view the return value from Future<bool>

Time:02-10

How can I view the return value from Future

 Future<bool> res;
 res = cancelDialog();
 print("res..........: ${res}");
 

my cancel dialog:

Future<bool> cancelDialog(_formKey, context, cancelFn) async {
  var res = await showModalActionSheet(
      onWillPop: () async => false,
      context: context,
      actions: <SheetAction>[
        SheetAction(label: "Evet", key: "evet"),
        SheetAction(label: "Hayır", key: "hayir")
      ],
      title: "Tüm değişilikler geri alınacaktır.",
      message: "Devam etmek istediğinize emin misiniz?");
  if (res == "evet") {
    _formKey.currentState?.reset();
     cancelFn.call();
     //return true;
     return Future<bool>.value(true);

  } else {
   //retun false;
    return Future<bool>.value(false);
  }
}

output : res..........: Instance of 'Future'

The value I want to see in the res is true or false, how can I provide it?

Thank YOU!

CodePudding user response:

you have to await to get the aswer of any value for example

bool dialogvalue=await res;
 print(dailogvalue)

CodePudding user response:

You are working with a Future<T> object from an async API, which is actually an object that will resolve (in a future) to your T.

Because of that, you need to either:

bool result = await res;
print(result); // true or false

or

res.then((result) => print(result)); // true or false

CodePudding user response:

To get the value of Future you have to add await keyword before that as below:

 Future<bool> res;
 res = await cancelDialog();
 print("res..........: ${res}");

Check it out and do let me know if you face the problem.

CodePudding user response:

The Answer is you have to use await keyword, it make the cancelDialog() future to finish then it proceeds to print function.

Future<bool> res;
 res = await cancelDialog();
 print("res..........: ${res}");

Make sure you use async keyword to the function of the above code

  • Related