【问题标题】:The return type 'Future<bool?> Function()' isn't a 'Future<bool>', as required by the closure's context根据闭包上下文的要求,返回类型“Future<bool?> Function()”不是“Future<bool>”
【发布时间】:2021-09-18 15:55:26
【问题描述】:
  • 我已将我的项目 sdk 升级到 &gt;2.12,因此我正在进行更改,但我遇到了这个特定错误,无法找到任何解决方案。
  • 我已经对方法进行了更改,如下所示,调用它时我不知道如何修复它。这个问题是在我升级并尝试解决所有non-nullable 或``null-safety``` 问题后引起的
  • 代码
  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();
                            },
                            ..
                            ..
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  • 错误

【问题讨论】:

    标签: flutter dart dart-null-safety


    【解决方案1】:

    你可以这样试试

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

    由于您的_onBackPressed 函数返回Future&lt;bool?&gt;, 和onWillPop 需要Future&lt;bool&gt;。 我所做的是使用result_onBackPressed 获得回报。返回可能是truefalsenull。所以我添加了一个空值检查,并将其更改为false。最后一点是关键,使用! 将可空的bool? 更改为boolresult! 表示您保证 result 不为空,即使您将其定义为可为空。

    【讨论】:

    • 只需将4th line 上的result 变量名称从retult 更正为result。它工作正常。如果您能详细说明它是如何解决错误的,那就太好了。
    • 我更新了我的答案,希望它阐述得足够清楚。
    【解决方案2】:

    onWillPop 需要 Future&lt;bool&gt;,这意味着返回值将是 true 或 false,但 _onBackPressed 的返回类型是 Future&lt;bool?&gt;,这意味着返回值是 true、false 或 null。将_onWillPop的返回类型从Future&lt;bool?&gt;更改为Future&lt;bool&gt;

    【讨论】:

    • 我认为这有助于更好地理解 null 安全的用法
    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 2020-10-14
    • 2021-07-08
    • 2021-11-24
    • 2021-10-16
    • 2021-10-10
    相关资源
    最近更新 更多