【问题标题】:Flutter A value of type 'Future<dynamic>' can't be returned from the method '_onBackPress' because it has a return type of 'Future<bool>'Flutter 无法从方法“_onBackPress”返回“Future<dynamic>”类型的值,因为它的返回类型为“Future<bool>”
【发布时间】:2021-09-27 22:17:37
【问题描述】:

我正在处理我的应用程序中的后退按钮,但我在颤振中遇到了这个问题 非常感谢先进 我以前使用此功能执行此操作,但现在无法正常工作 我认为是因为 null 安全 对不起,我的英语不好 screenshot

这是我的颤振医生

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.2.3, on Microsoft Windows [Version 10.0.19043.1110], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[√] Chrome - develop for the web
[√] Android Studio (version 4.1.0)
[√] VS Code (version 1.58.2)
[√] Connected device (3 available)

• No issues found!

这是我的构建方法和Future函数

Future<bool> _onBackPress() {
    return showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          content: Text('Exit From The App'),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop(false);
              },
              child: Text('No'),
            ),
            TextButton(
              onPressed: () {
                Navigator.of(context).pop(true);
              },
              child: Text('Yes'),
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onBackPress,
      child: Scaffold(
        appBar: AppBar(
          automaticallyImplyLeading: false,
          brightness: Brightness.dark,
          title: Text('Internet Availability Checker'),
        ),
      ),
    );
  }

【问题讨论】:

    标签: flutter flutter-layout flutter-test


    【解决方案1】:

    showDialog 是一个异步函数,这意味着它会等到某个结果。在这种情况下 - 直到对话框关闭。因为它是异步的,所以您可以在 showDialog 右括号之后添加 .then(意思是“收到值时”)。

    .then((value) => value ?? false);

    顺便说一句,默认情况下“value”为空,当您尝试在对话框区域外点击以处理它时,它将为空。为了避免空返回使用'??'检查将默认值(如果为null)设置为false

     Future<bool> _onBackPress() {
        return showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              content: Text('Exit From The App'),
              actions: [
                TextButton(
                  onPressed: () {
                    Navigator.of(context).pop(false);
                  },
                  child: Text('No'),
                ),
                TextButton(
                  onPressed: () {
                    Navigator.of(context).pop(true);
                  },
                  child: Text('Yes'),
                ),
              ],
            );
          },
        ).then((value) => value ?? false);
      }
    

    【讨论】:

      【解决方案2】:

      在您的 _onBackPress() 函数中,您没有返回布尔值。应该这样改写:

      Future<bool> _onBackPress() async {
        bool goBack = false;
        await showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              content: Text('Exit From The App'),
              actions: [
                TextButton(
                  onPressed: () {
                    goBack = false;
                    Navigator.pop(context);
                  },
                  child: Text('No'),
                ),
                TextButton(
                  onPressed: () {
                    goBack = true;
                    Navigator.pop(context);
                  },
                  child: Text('Yes'),
                ),
              ],
            );
          },
        );
        return goBack;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-26
        • 2022-07-13
        • 1970-01-01
        相关资源
        最近更新 更多