【问题标题】:The argument type 'Future<bool>' can't be assigned to the parameter type 'Future<bool> Function()?'参数类型“Future<bool>”不能分配给参数类型“Future<bool> Function()?”
【发布时间】:2021-10-10 06:20:15
【问题描述】:

我需要在设备上编程后退箭头,但出现此错误。

WillPopScope 的代码: 正文:WillPopScope( onWillPop:返回(上下文),=> 错误 ),

后退功能是:

Future<bool> back(BuildContext context) async {
    Navigator.of(context).pop();
    return true;
  }

【问题讨论】:

    标签: flutter


    【解决方案1】:

    改变

     onWillPop: back(context),
    

    onWillPop: () async => await back(context),
    

    【讨论】:

    • 它有效,但为什么会发生这种情况? 'Future Function() 类型是什么意思? @Hemal
    • onWillPop 正在返回 WillPopCallback?,这是 'Future&lt;bool&gt; Function()? 的 typedef。它用于为函数创建用户定义的身份(别名)。在这种情况下,您不能直接使用未来的功能。
    • 当我将此函数用作此类的成员时,它直接调用,但是当我将其作为单独文件中的全局变量时,问题发生了,@Hemal
    【解决方案2】:

    根据您的要求,您将单击后退箭头并转到上一个屏幕,因此请尝试以下代码,希望对您有所帮助。

    IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () => Navigator.pop(context, false),
        ),
    

    【讨论】:

    • 这两样我都需要做
    • 你想简单解释什么
    • 有时您需要对后退箭头进行编程,因为当设备的语言是从右到左时,您需要手动对其进行编程。解决方案由另一个人回答,感谢您帮助我:) @Ravindra S. Patil
    猜你喜欢
    • 2021-10-16
    • 2021-11-24
    • 2020-08-23
    • 2021-04-07
    • 2021-07-06
    • 1970-01-01
    • 2022-01-17
    • 2021-08-11
    • 1970-01-01
    相关资源
    最近更新 更多