Lav Sharma
Lav Sharma

Reputation: 339

The return type 'Future<bool?> Function()' isn't a 'Future<bool>', as required by the closure's context

  Future<bool?> _onBackPressed() async {
    return showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('Do you want to exit without saving changes?'),
            content:
                Text('Please press the SAVE button at the bottom of the page'),
            actions: <Widget>[
              TextButton(
                child: Text('NO'),
                onPressed: () {
                  Navigator.of(context).pop(false);
                },
              ),
              TextButton(
                child: Text('YES'),
                onPressed: () {
                  Navigator.of(context).pop(true);
                  Navigator.of(context).pop(true);
                },
              ),
            ],
          );
        });
  }
..
    return WillPopScope(
      onWillPop: () => _onBackPressed, // causing error
      child: Scaffold(
        key: _scaffoldkey,
        body: SafeArea(
          child: SingleChildScrollView(
            child: Container(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Stack(
                    children: <Widget>[
                      Positioned(
                        child: AppBar(
                          centerTitle: true,
                          title: Text(
                            "Sample",
                            style: TextStyle(
                              fontFamily: 'LobsterTwo',
                              fontStyle: FontStyle.italic,
                              fontWeight: FontWeight.bold,
                              fontSize: 18.0,
                              color: Colors.black87,
                            ),
                          ),
                          backgroundColor: Colors.transparent,
                          elevation: 0,
                          leading: IconButton(
                            icon: Icon(
                              Icons.arrow_back,
                              color: Colors.black,
                              size: 30,
                            ),
                            onPressed: () {
                              _onBackPressed();
                            },
                            ..
                            ..
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );

enter image description here

Upvotes: 4

Views: 15858

Answers (2)

Sam Chan
Sam Chan

Reputation: 1762

You can try like this

  onWillPop: () async {
          bool? result= await _onBackPressed();
          if(result == null){
             result = false;
          }
          return result!;
        }, 

Since your _onBackPressed function return Future<bool?>, and onWillPop require a Future<bool>. what I do is use result to get your return from _onBackPressed. The return may be true false or null.So I add a null checking for null value and change it to false. Last thing is the key, use ! to change the nullable bool? to bool. result! means you guarantee that result is not null even you defined it nullable.

Upvotes: 12

Sanmay Kant
Sanmay Kant

Reputation: 159

onWillPop expects a Future<bool> which means return value will either be true or false but the return type of _onBackPressed is Future<bool?> which means the return value be either true, false or null. Change the return type of _onWillPop from Future<bool?> to Future<bool>.

Upvotes: 4

Related Questions