【问题标题】:How to make a named method parameter required Flutter?如何使命名方法参数需要 Flutter?
【发布时间】:2021-01-12 13:28:15
【问题描述】:
void checkIfAllValsAreIn(
    { Map<> map,
    bool isBool,
    Function() callback}) async {...}

如何使这些命名参数中的任何一个在调用时成为必需参数?

【问题讨论】:

    标签: flutter dart methods parameters named


    【解决方案1】:

    让他们@required

    import 'package:meta/meta.dart';
    void checkIfAllValsAreIn(
        { @required Map<> map,
        @required bool isBool,
        @required Function() callback}) async {...}
    

    【讨论】:

      【解决方案2】:

      尝试在正文中添加:

      assert(isBool != null);
      

      【讨论】:

      • 这不起作用,但第一个答案可以。谢谢。
      • 1.这将是运行时检查,而不是构建时检查。 2.参数是否可以为空可以独立于它是否是必需的。例如,您可能希望要求调用者始终提供参数,但允许它们显式传递 null。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 2013-09-22
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      • 2022-11-01
      相关资源
      最近更新 更多