【问题标题】:Dart - How to have class initializer functions work with constructors?Dart - 如何让类初始化函数与构造函数一起使用?
【发布时间】:2020-10-05 17:10:57
【问题描述】:

如果我通过构造函数创建实例,初始化函数将被忽略。如何让初始化函数也适用于构造函数? 这是我如何称呼班级

User fireBaseUser = new User("12345","Test Name"); // shortenedName exists
var snap = {"uid" : "12345", "displayName" : "Test Name"};
User fireBaseUser = User.fromSnapshot(snap); // shortenedName wont exist

class User {
  final String uid;
  final String fireBaseDisplayName;
  String shortenedName;

  User.fromSnapshot( DocumentSnapshot document)
      : uid = snapshot.documentID,
        fireBaseDisplayName = snapshot['displayName'];

  User(
      {this.uid,
      this.fireBaseDisplayName,
      this.shortenedName,
     }) {
    shortenName(fireBaseDisplayName);
  }

  shortenName(fireBaseDisplayName) {
    shortenedName =
        fireBaseDisplayName.substring(0, fireBaseDisplayName.indexOf(' '));
  }

只有当我像这样复制初始化函数时,构造函数似乎才能工作

class User {
  final String uid;
  final String fireBaseDisplayName;
  String shortenedName;

  User.fromSnapshot( DocumentSnapshot document)
      : uid = snapshot.documentID,
        fireBaseDisplayName = snapshot['displayName'];
        shortenedName = snapshot['displayName'].substring(0, snapshot['displayName'].indexOf(' '));

  User(
      {this.uid,
      this.fireBaseDisplayName,
      this.shortenedName,
     }) {
    shortenName(fireBaseDisplayName);
  }

  shortenName(fireBaseDisplayName) {
    shortenedName =
        fireBaseDisplayName.substring(0, fireBaseDisplayName.indexOf(' '));
  }

相关How to initialize a class' fields with a function in dart?

【问题讨论】:

标签: flutter dart


【解决方案1】:

你使用了'named parameter {}',所以你应该使用参数名。

1) 存在缩短的名称 用户 fireBaseUser = new User(uid: "12345",fireBaseDisplayName: "测试名称", shortedName: "TN");

2) 缩短的名称不存在 用户 fireBaseUser = new User(uid: "12345",fireBaseDisplayName: "Test Name");

class User {
  final String uid;
  final String fireBaseDisplayName;
  String shortenedName;

  User({
      this.uid, 
      this.fireBaseDisplayName,
      this.shortenedName,
       });

【讨论】:

    【解决方案2】:

    这是generative constructor

    User(
          {this.uid,
          this.fireBaseDisplayName,
          this.shortenedName,
         }) {
        shortenName(fireBaseDisplayName);
      }
    

    这是named constructor

    User.fromSnapshot( DocumentSnapshot document)
          : uid = snapshot.documentID,
            fireBaseDisplayName = snapshot['displayName'];
    

    在对象初始化User fireBaseUser = User.fromSnapshot(snap); 中,您正在调用named constructor,因此您必须在您正在调用的named constructor 中调用有趣的shortenName(fireBaseDisplayName);,如下所示:

    User.fromSnapshot( DocumentSnapshot document)
          : uid = snapshot.documentID, // snapshot or document?    :)
            fireBaseDisplayName = snapshot['displayName'] {
              shortenName(fireBaseDisplayName);
          }
    
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多