【问题标题】:Flutter: Default assignment of List parameter in a constructorFlutter:构造函数中List参数的默认分配
【发布时间】:2019-01-20 17:49:37
【问题描述】:

是否可以在定义构造函数时将常量值分配给数据类型 List 的可选参数。 例如,

`class sample{
  final int x;
  final List<String> y;
  sample({this.x = 0; this.y = ["y","y","y","y"]});
 }`

上面的代码抛出一个错误,集中在 y 赋值上,说 Default values of an optional parameter must be constant

这个错误的原因是什么? 我还能如何为列表分配默认值?

【问题讨论】:

  • ["y","y","y","y"]改成const ["y","y","y","y"]

标签: dart flutter


【解决方案1】:

默认值当前需要是 const。这在未来可能会改变。

如果你的默认值可以是 const,添加 const 就足够了

class sample{
  final int x;
  final List<String> y;
  sample({this.x = 0; this.y = const ["y","y","y","y"]});
}

当需要const 时,Dart 通常只假设const,但对于默认值,这被省略,以免在实际删除约束时破坏现有代码。

如果你想要一个不能为 const 的默认值,因为它是在运行时计算的,你可以在初始化列表中设置它

class sample{
  final int x;
  final List<String> y;
  sample({this.x = 0; List<String> y}) : y = y ?? ["y","y","y","y"];
}

【讨论】:

  • 如果列表在一个单独的文件中,我们将如何分配 this.y = ?
  • @Gunter Zochbauer - Jag99 遇到了同样的问题。如果有的话,请分享您的建议。谢谢。
  • 抱歉,不知道是什么问题。如果它在另一个文件中,则需要导入该文件,并且在另一个文件中它需要是公共的,以便可以从导入文件中访问它。我建议您创建一个新问题,其中包含重现所需的所有信息。
【解决方案2】:

对于带有 Named 或 Positional 参数的构造函数,我们可以使用 = 来定义默认值。

默认值必须是编译时常量。如果我们不提供值,则默认值为 null。

位置可选参数

class Customer {
  String name;
  int age;
  String location;

  Customer(this.name, [this.age, this.location = "US"]);

  @override
  String toString() {
    return "Customer [name=${this.name},age=${this.age},location=${this.location}]";
  }
}

现在创建一些Customer 对象,您可以看到age 的默认值为nulllocation 的默认值为"US"

var customer = Customer("bezkoder", 26, "US");
print(customer);
// Customer [name=bezkoder,age=26,location=US]

var customer1 = Customer("bezkoder", 26);
print(customer1);
// Customer [name=bezkoder,age=26,location=US]

var customer2 = Customer("zkoder");
print(customer2);
// Customer [name=zkoder,age=null,location=US]

命名的可选参数

class Customer {
  String name;
  int age;
  String location;

  Customer(this.name, {this.age, this.location = "US"});

  @override
  String toString() {
    return "Customer [name=${this.name},age=${this.age},location=${this.location}]";
  }
}

让我们运行检查agelocation 的默认值。

var customer = Customer("bezkoder", age: 26, location: "US");
print(customer);
// Customer [name=bezkoder,age=26,location=US]

var customer1 = Customer("bezkoder", age: 26);
print(customer1);
// Customer [name=bezkoder,age=26,location=US]

var customer2 = Customer("zkoder");
print(customer2);
// Customer [name=zkoder,age=null,location=US]

常量构造函数

如果我们希望我们类的所有实例永远不会改变,我们可以定义一个 const 构造函数,其中所有字段都是final

class ImmutableCustomer {
  final String name;
  final int age;
  final String location;

  // Constant constructor
  const ImmutableCustomer(this.name, this.age, this.location);
}

现在我们可以将const关键字放在构造函数名称之前:

var immutableCustomer = const ImmutableCustomer("zkoder", 26, "US");
// immutableCustomer.name = ... // compile error

参考:Constructor default value

【讨论】:

    【解决方案3】:

    非常老的问题,但由于它仍然可以通过 Google 找到,所以像我这样的人仍然可以找到它。 Flutter 现在为 List 类提供了一个新的构造函数来执行此操作:

    final List<String> y = List.filled(4, 'y');
    

    这将创建一个包含四个元素的列表,所有元素的值都设置为“y”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-10
      • 2022-01-04
      • 2012-06-30
      • 2014-07-09
      • 1970-01-01
      • 2012-02-26
      • 2011-11-05
      • 2016-06-05
      相关资源
      最近更新 更多