简体   繁体   English

Dart 为参数设置默认值

[英]Dart set default value for parameter

in Flutter framework i'm trying to set default value for parameters as borderRadius , in this sample how can i implementing that?Flutter框架中,我试图将参数的默认值设置为borderRadius ,在这个示例中我如何实现它? i get Default values of an optional parameter must be constant error when i try to set that, for example:当我尝试设置时,我得到Default values of an optional parameter must be constant错误,例如:

class SimpleRoundButton extends StatelessWidget {
  final BorderRadius borderRadius;
  SimpleRoundButton({
  }):this.borderRadius = BorderRadius.circular(30.0);
}


class SimpleRoundButton extends StatelessWidget {
  final BorderRadius borderRadius= BorderRadius.circular(30.0);
  SimpleRoundButton({
    this.borderRadius,
  });
}


class SimpleRoundButton extends StatelessWidget {
  final BorderRadius borderRadius;
  SimpleRoundButton({
    this.borderRadius=  BorderRadius.circular(30.0)
  });
}

all of this samples are incorect所有这些样本都是不正确的

BorderRadius.circular() is not a const function so you cannot use it as a default value. BorderRadius.circular()不是 const 函数,因此您不能将其用作默认值。

To be able to set the const circular border you can use BorderRadius.all function which is const like below:为了能够设置常量圆形边框,您可以使用BorderRadius.all函数,其常量如下所示:

class SimpleRoundButton extends StatelessWidget {
  final BorderRadius borderRadius;
  SimpleRoundButton({
    this.borderRadius: const BorderRadius.all(Radius.circular(30.0))
  });

  @override
  Widget build(BuildContext context) {
    return null;
  }
}

Gunhan's answer explained how you can set a default BorderRadius . Gunhan 的回答解释了如何设置默认BorderRadius

In general, if there isn't a const constructor available, you instead can resort to using a null default value (or some other appropriate sentinel value) and then setting the desired value later:通常,如果没有可用的const构造函数,您可以改为使用null默认值(或其他一些适当的标记值),然后稍后设置所需的值:

class Foo {
  Bar bar;

  Foo({Bar? bar}) : bar = bar ?? Bar();
}

Note that explicitly passing null as an argument will do something different with this approach than if you had set the default value directly.请注意,与直接设置默认值相比,使用此方法显式传递null作为参数会有所不同。 That is, Foo(bar: null) with this approach will initialize the member variable bar to Bar() , whereas with a normal default value it would be initialized to null and require that the member be nullable.也就是说,使用这种方法的Foo(bar: null)会将成员变量bar初始化为Bar() ,而对于正常的默认值,它将被初始化为null并要求该成员可以为空。 (In some cases, however, this approach's behavior might be more desirable.) (但是,在某些情况下,这种方法的行为可能更受欢迎。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dart 中 Function 类型的参数默认值 - Default value for parameter of Function type in Dart 如何使用默认值设置 dart 中的列表长度? - How to set length of list in dart with default value? Dart:使用非常量值作为默认构造函数参数 - Dart: Use non-constant value as default constructor parameter Dart Flutter:为类构造函数设置默认值时,可选参数的默认值必须是常量 - Dart Flutter: The default value of an optional parameter must be constant when setting a default value to class constructor Flutter ,dart,参数 'colour' 因为它的类型不能有 'null' 的值,但是隐含的默认值是 'null' - Flutter ,dart,The parameter 'colour' can't have a value of 'null' because of its type, but the implicit default value is 'null' Dart 中字符串的默认值是多少? - What is the default value for a String in Dart? 如何最好地转换触发“参数不能为 'null' 但隐式默认值为 'null' 的旧 Dart 代码。” 错误? - How best to convert old Dart code that triggers "parameter can't be 'null' but the implicit default value is 'null'." error? Dart:如何创建一个空列表作为默认参数 - Dart: how to create an empty list as a default parameter 如何在 dart 中将默认参数分配给除 map 参数之外的函数 - How to assign a default parameter to a function aside of map parameter in dart flutter dart 上对象的参数值 - value of parameter from object on flutter dart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM