Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • I have upgraded my project sdk to >2.12 so I am making the changes and I am stuck at this particular error and not able to find any solution for it.
  • I have made changes in the method as you can see below while calling it I am not sure how to fix it. The issue is being caused after I upgraded and trying to solve all the non-nullable or ``null-safety``` issue
  • Code
  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();
                            },
                            ..
                            ..
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  • Error

enter image description here

like image 301
Lav Sharma Avatar asked Oct 29 '25 05:10

Lav Sharma


2 Answers

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.

like image 188
Sam Chan Avatar answered Oct 30 '25 23:10

Sam Chan


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>.

like image 35
Sanmay Kant Avatar answered Oct 30 '25 21:10

Sanmay Kant