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?
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 .
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.
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.
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.
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(...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With