【问题标题】:Flutter: The argument type 'Future<bool?> Function()' can't be assigned to the parameter type 'Future<bool> Function()?'Flutter:参数类型'Future<bool> Function()' 不能分配给参数类型'Future<bool> Function()?'
【发布时间】:2021-11-24 04:07:29
【问题描述】:

我为退出弹出服务创建了一个WillPopScope 函数。当用户点击后退按钮时,它会显示一个退出权限对话框。

它在 Flutter 版本 1 上完美运行。现在对于 null 问题,这是一个问题。

这是我的代码:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class ExitPopUp extends StatelessWidget {
  final page;
  ExitPopUp(this.page);
  @override
  Widget build(BuildContext context) {
    Future<bool?> showExitPopUp() {
      return showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(20.0))),
              backgroundColor: Color.fromRGBO(0, 68, 69, .5),
              title: Text("Confirm", style: TextStyle(color: Colors.white)),
              content: Text("Do you want to Exit ?",
                  style: TextStyle(color: Colors.white)),
              actions: <Widget>[
                ElevatedButton(
                    child: Text(
                      "No",
                      style: TextStyle(color: Colors.yellow[100]),
                    ),
                    onPressed: () {
                      Navigator.pop(context, false);
                    }),
                ElevatedButton(
                    child: Text("Yes",
                        style: TextStyle(color: Colors.yellow[100])),
                    onPressed: () {
                      SystemNavigator.pop();
                    })
              ],
            );
          });
    }

    return WillPopScope(child: page, onWillPop: showExitPopUp);
  }
}

但这是关于 onWillPop 的一个问题。

我该如何解决这个问题?

【问题讨论】:

  • 您说它在可空更改之前有效。是什么让您在 Future&lt;bool?&gt; showExitPopUp() { 行中添加了 ?
  • 您可以关注此 SO 帖子。 stackoverflow.com/questions/68297278/…
  • 我这次添加 Future

标签: flutter popup exit nullable


【解决方案1】:

您可以将您的 WillPopScope 更改为:

return WillPopScope(child: page, onWillPop: () => showExitPopUp().then(b -> b ?? false));

或者您可以将您的 showExitPopUpFuture&lt;bool?&gt; 更改为 Future&lt;bool&gt;

【讨论】:

  • 谢谢。我只是将它转换为异步函数。并且成功了
【解决方案2】:

我只是将它转换为异步函数。它奏效了 这是更新的代码

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class ExitPopUp extends StatelessWidget {
  final page;
  ExitPopUp(this.page);
  @override
  Widget build(BuildContext context) {
    Future<bool> showExitPopUp() async {
      return await showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(20.0))),
              backgroundColor: Color.fromRGBO(0, 68, 69, .5),
              title: Text("Confirm", style: TextStyle(color: Colors.white)),
              content: Text("Do you want to Exit ?",
                  style: TextStyle(color: Colors.white)),
              actions: <Widget>[
                ElevatedButton(
                    child: Text(
                      "No",
                      style: TextStyle(color: Colors.yellow[100]),
                    ),
                    onPressed: () {
                      Navigator.pop(context, false);
                    }),
                ElevatedButton(
                    child: Text("Yes",
                        style: TextStyle(color: Colors.yellow[100])),
                    onPressed: () {
                      SystemNavigator.pop();
                    })
              ],
            );
          });
    }

    return WillPopScope(child: page, onWillPop: showExitPopUp);
  }
}

【讨论】:

    猜你喜欢
    • 2021-10-16
    • 2021-10-10
    • 1970-01-01
    • 2020-08-23
    • 2021-04-07
    • 2021-07-06
    • 2022-01-17
    • 2021-08-11
    • 1970-01-01
    相关资源
    最近更新 更多