giorgio79
giorgio79

Reputation: 4239

Dart - How to have class initializer functions work with constructors?

Initializer functions are ignored if I create an instance through a constructor. How to have initializer functions work on constructors as well? Here is how I call the class

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(' '));
  }

Constructor only seems to work if I duplicate the initializer funciton like this

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(' '));
  }

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

Upvotes: 0

Views: 194

Answers (2)

Mohammed Adam
Mohammed Adam

Reputation: 409

this is a generative constructor :

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

and this is a named constructor :

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

in the object initialize User fireBaseUser = User.fromSnapshot(snap); you are calling the named constructor so you have to call the fun shortenName(fireBaseDisplayName); inside the named constructor you are calling, like this :

User.fromSnapshot( DocumentSnapshot document)
      : uid = snapshot.documentID, // snapshot or document?    :)
        fireBaseDisplayName = snapshot['displayName'] {
          shortenName(fireBaseDisplayName);
      }

Upvotes: 1

KuKu
KuKu

Reputation: 7512

You used 'named parameter {}', so you should use parameter name.

1) shortenedName exists User fireBaseUser = new User(uid: "12345",fireBaseDisplayName: "Test Name", shortenedName: "TN");

2) shortenedName not exists User 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,
       });

Upvotes: 0

Related Questions