简体   繁体   中英

Flutter Dart | How to return different object from class Constructor

In Dart , is it possible for a constructor to cancel object creation and return a different object instead?

Use case: Users contains a static map that maps ids to User objects. When a User is initialized, I want the User constructor to check if User with id is already created, if so: return existing User object , else create a new User object

Example (of-course not working):

class Users {
  static const Map<String, User> users = {};
}

class User {
 final String id;
 final String firstName;

 User({required id, required firstName}) {
  // If user with id already exists, return that object
  if (Users.users.containsKey(id) {
    return Users.users[id];
  }
 // Else, initialize object and save it in Users.users
  this.id = id; 
  this.firstName = firstName;
  Users.users[id] = this;
 }
}

Question: IS there any way to get the above pseudo code to work?

As mentioned by jamesdlin you should use a factory constructor. Here's what is mentioned in the documentation :

Use the factory keyword when implementing a constructor that doesn't always create a new instance of its class.

And in your case this is exactly what you want. Now here's a code sample that does what you want:

Code sample

class Users {
  // Also your Map cannot be const if you want to edit it.
  static Map<String, User> users = {};
}

class User {
  final String id;
  final String firstName;

  /// Base private constructor required to create the User object.
  User._({required this.id, required this.firstName});

  /// Factory used to create a new User if the id is available otherwise return the User
  /// associated with the id.
  factory User({required String id, required String firstName}) {
    // If user with id already exists, return that object
    if (Users.users.containsKey(id)) {
      // Force casting as non nullable as we already checked that the key exists
      return Users.users[id]!;
    }
    
    // Else, initialize object and save it in Users.users
    final newUser = User._(id: id, firstName: firstName);
    Users.users[id] = newUser;
    return newUser;
  }
}

Try the full code on DartPad

You can create a function in class to handle things you want. Here's what you can implement.

class Player {
  final String name;
  final String color;

  Player(this.name, this.color);

  Player.fromPlayer(Player another) :
    color = another.color,
    name = another.name;
}

If this is for caching purposes or you are not creating multiple instances of the Users class, I would suggest using a pattern where static is responsible for a list of class instances. Sometimes this helps to significantly reduce the amount of code:

class User {
  static final Map<String, User> users = {};
  
  final String id, firstName;

  User._({required this.id, required this.firstName});
  
  factory User({required String id, required String firstName}) => users[id] ??= User._(id: id, firstName: firstName);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM