【问题标题】:Constructor Optional Params构造函数可选参数
【发布时间】:2019-02-26 04:54:59
【问题描述】:

有没有办法设置构造函数可选参数? 我的意思是:

User.fromData(this._name, 
  this._email, 
  this._token, 
  this._refreshToken,  
  this._createdAt,  
  this._expiresAt,  
  this._isValid,  
  {this.id});

表示

命名选项参数不能以下划线开头。

但是我需要这个字段作为私有字段,所以,我现在迷路了。

【问题讨论】:

  • 使用 [] 代替 {}
  • 用 [ ] 定义的私有变量有什么用,请建议。谢谢。

标签: dart flutter


【解决方案1】:

对于未来的观众来说,这是一个更普遍的答案。

位置可选参数

将可选参数用[ ] 方括号括起来。

class User {

  String name;
  int age;
  String home;

  User(this.name, this.age, [this.home = 'Earth']);
}

User user1 = User('Bob', 34);
User user2 = User('Bob', 34, 'Mars');

如果你不提供默认值,可选参数需要可以为空:

class User {

  String name;
  int age;
  String? home; //    <-- Nullable

  User(this.name, this.age, [this.home]);
}

命名可选参数

{ } 花括号将可选参数括起来。

class User {

  String name;
  int age;
  String home;

  User(this.name, this.age, {this.home = 'Earth'});
}

User user1 = User('Bob', 34);
User user2 = User('Bob', 34, home: 'Mars');

home 的默认值是“地球”,但和以前一样,如果您不提供默认值,则需要将 String home 更改为 String? home

私人领域

如果您需要私有字段,则可以使用[] 方括号:

class User {
  int? _id;
  User([this._id]);
}

User user = User(3);

或者按照公认的答案执行并使用初始化列表:

class User {
  int? _id;
  User({int? id}) 
    : _id = id;
}

User user = User(id: 3);

命名的必需参数

命名参数默认是可选的,但是如果你想让它们成为必需的,那么你可以使用required关键字:

class User {
  final String name;
  final int age;
  final String home;

  User({
    required this.name,
    required this.age,
    this.home = 'Earth',
  });
}

User user1 = User(name: 'Bob', age: 34);
User user2 = User(name: 'Bob', age: 34, home: 'Mars');

【讨论】:

  • 大小写可选命名参数 - 我在颤振中使用 dart 类,代码如下:class MyDataObject { final int anInt;最终字符串 aString;最终双 aDouble; MyDataObject({ this.anInt = 1, this.aString = 'Old!', this.aDouble = 2.0, });在 this.anInt = 1, this.aString = 'Old!' 之前出现需要“添加所需关键字”的错误并且 this.aDouble = 2.0,请提出问题所在以及我们如何解决它。谢谢。
  • @Kamlesh,我不确定您为什么会收到该错误。如果您愿意,您可以在它们之前添加 required 关键字,但由于您提供了默认值,因此您不需要这样做。仅在删除默认值时才需要。
  • 我已经扩展了答案以包括命名的必需参数以及为 null 安全进行更新。
  • 类用户{字符串名称;年龄;串回家;用户(this.name, this.age, {this.home = 'Earth'}); } 用户 user1 = User('Bob', 34);用户 user2 = User('Bob', 34, home: 'Mars');命名可选参数示例让我很开心。有用。谢谢亲爱的Suragch
【解决方案2】:

您需要使用一个简单的参数并在初始化列表中初始化您的私有字段。

class User {
  final String _id;
  final String _name;
  User.fromData(this._name, {required String id})
      : _id = id;
}

【讨论】:

  • 大小写可选命名参数 - 我在颤振中使用 dart 类,代码如下:class MyDataObject { final int anInt;最终字符串 aString;最终双 aDouble; MyDataObject({ this.anInt = 1, this.aString = 'Old!', this.aDouble = 2.0, });在 this.anInt = 1, this.aString = 'Old!' 之前出现需要“添加所需关键字”的错误并且 this.aDouble = 2.0,请提出问题所在以及我们如何解决它。谢谢。
  • 您会遇到此问题,因为该语言现在是空安全的(您必须为可空类型添加 ? 或在不可空命名参数之前使用 required)。我更新了代码 sn-p 以使其与 nullsafety 一起使用。
  • 感谢亚历山大的回复。我在类中寻找可选的命名参数,并在stackoverflow.com/a/55769496/10329023 处获得了满足我要求的正确解决方案
【解决方案3】:

除了伟大的Suragch 的answer,我还想提一下required 这个词。您可以将它用于多个构造函数或函数命名参数以指定所需的参数。

class User {
  int _id;
  String _firstName;
  String _lastName;
  User({required int id, String firstName = "", String lastName}) 
    : _id = id,  // required parameter
      _firstName = firstName,  // optional parameter with default value ""
      _lastName = lastName;  // optional parameter without default value
}

User user1 = User(id: 1);
User user2 = User(id: 2, firstName: "John");
User user3 = User(id: 3, lastName: "Snow");

相关 Dart 文档 here.

对于 Dart 版本 @required 是一个注解并与 @ 前缀一起使用。

【讨论】:

  • 大小写可选命名参数 - 我在颤振中使用 dart 类,代码如下:class MyDataObject { final int anInt;最终字符串 aString;最终双 aDouble; MyDataObject({ this.anInt = 1, this.aString = 'Old!', this.aDouble = 2.0, });在 this.anInt = 1, this.aString = 'Old!' 之前出现需要“添加所需关键字”的错误并且 this.aDouble = 2.0,请提出问题所在以及我们如何解决它。谢谢。
猜你喜欢
  • 1970-01-01
  • 2021-03-12
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-04
  • 1970-01-01
  • 2023-01-31
相关资源
最近更新 更多