【问题标题】:flutter : willpopscope, The return type 'future<bool?>' isn't 'future<bool>颤振:willpopscope,返回类型 'future<bool?>' 不是 'future<bool>
【发布时间】:2021-08-11 15:47:41
【问题描述】:

我已经对方法进行了更改,如下所示,调用它时我不知道如何修复它。这个问题是在我升级并尝试解决所有不可空或“空安全”后引起的

@override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: ()=> showDialog<bool>(context: context,
          builder: (context)=> AlertDialog(
            title: Text('Alert'),
            content: Text('Voulez vous quitter l\'application ?'),
            actions: [
              TextButton(onPressed:(){
                if (Platform.isAndroid) {
                  SystemNavigator.pop();
                } else if (Platform.isIOS) {
                  exit(0);
                }
              },
                  child: Text('Oui')),
              TextButton(onPressed: () => Navigator.of(context).pop(false),
                  child: Text('Non')
              )
            ],
          )
      ),
          child: Scaffold()
}

这里是错误错误:返回类型“Future”不是“Future”,这是闭包上下文所要求的。

我该如何解决这个问题?

【问题讨论】:

标签: flutter dart


【解决方案1】:

基本上问题是showDialog 函数可以返回一个null 值,例如用户通过单击其背景关闭对话框。同时,onWillPopScope 需要一个不可为空的布尔值。 (准确地说,在这两种情况下,我们都在谈论 Futures 返回可为空或非布尔值,但这不是这里的问题)

所以解决方案不是返回 showDialog 结果,而只是返回 showDialog 结果与 true 相比,这样 null 将变为 false

@override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: ()=> showDialog<bool>(context: context,
          builder: (context)=> AlertDialog(
            title: Text('Alert'),
            content: Text('Voulez vous quitter l\'application ?'),
            actions: [
              TextButton(onPressed:(){
                if (Platform.isAndroid) {
                  SystemNavigator.pop();
                } else if (Platform.isIOS) {
                  exit(0);
                }
              },
                  child: Text('Oui')),
              TextButton(onPressed: () => Navigator.of(context).pop(false),
                  child: Text('Non')
              )
            ],
          )
      ) == true,
          child: Scaffold()
}

【讨论】:

  • 我还没有测试过代码,但它现在应该可以工作了
【解决方案2】:

试试这个让我知道它是否适合你

 Future<bool> _onClose() async {
    return await showDialog(
        context: context,
        builder: (context) => AlertDialog(
              title: Text('Alert'),
              content: Text('Voulez vous quitter l\'application ?'),
              actions: [
                TextButton(
                    onPressed: () {
                      if (Platform.isAndroid) {
                        SystemNavigator.pop();
                      } else if (Platform.isIOS) {
                        exit(0);
                      }
                    },
                    child: Text('Oui')),
                TextButton(
                    onPressed: () => Navigator.of(context).pop(false),
                    child: Text('Non'))
              ],
            ));
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onClose,
      child: Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
      ),
    );
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-01
    • 2020-07-04
    • 2019-07-08
    • 2021-10-14
    • 2021-09-18
    • 2023-04-11
    • 2020-09-11
    • 2021-05-22
    相关资源
    最近更新 更多