Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return Future or any other types from a function in dart

Tags:

I need to handle the back button press on one of my Form entry widgets. This is how I implemented the WillPopScope's onWillPop method :

Future<bool> _onWillPop() {     if (changed) {        return showDialog(         context: context,         builder: (context) => new AlertDialog(               title: new Text('Save'),               content: new Text("Do you want to save the changes?"),               actions: <Widget>[                 new FlatButton(                   onPressed: () => Navigator.of(context).pop(true),                   child: new Text('No'),                 ),                 new FlatButton(                   onPressed: () {                     Navigator.of(context).pop(false);                     saveMeeting();                   },                   child: new Text('Yes'),                 ),               ],             ),       ) ??       false;     } else {       print("No changes");       Navigator.of(context).pop(true);       //return some future null from here ????     } } 

This part of the code is working but getting an exception:

[ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception: E/flutter ( 7374): Failed assertion: boolean expression must not be null 

How can I implement this correctly?

like image 897
krishnakumarcn Avatar asked Jul 23 '18 12:07

krishnakumarcn


People also ask

How do you return a Future value in flutter?

get() method returns a Future object: Calling a function that returns a Future, will not block your code, that's why that function is called asynchronous. Instead, it will immediately return a Future object, which is at first uncompleted. Future<T> means that the result of the asynchronous operation will be of type T .

How do you write a Future function in darts?

Solution 1: Future() constructor The most basic solution is to use the generative constructor of Future . I changed the return type of the method to Future<int> and then passed in the work of the old function as an anonymous function to the Future constructor.

How do you call Future method in flutter?

There are two different ways to execute a Future and utilize the value it returns. If it returns any whatsoever. The most well-known way is to await on the Future to return. For everything to fall into work, your function that is calling the code must be checked async.

What is async function in Dart?

Summary of asynchronous programming in Dart Asynchronous function is a function that returns the type of Future. We put await in front of an asynchronous function to make the subsequence lines waiting for that future's result.


2 Answers

I had exactly the same problem.

I solved it by returning Future.value(false); If the value is true I get the black screen.

Future<bool> _onBackPressed() {  if (!widget.editing) {    Navigator.pop(context, true);    return Future.value(false);  } else {    return showDialog(... 
like image 151
csigueros Avatar answered Sep 25 '22 20:09

csigueros


Import "dart:async" package, and add async keyword to your method signature like

Future<bool> _onWillPop() async{ 

After this, you will just need to return a boolean value whenever your method completes its processing just like any other function

like image 33
Ganapat Avatar answered Sep 22 '22 20:09

Ganapat