title: Flutter Dart | How to return different object from class Constructor
body: <p>In <code>Dart</code>, is it possible for a <code>constructor</code> to cancel object creation and return a different object instead?</p>
  <p>Use case:
  <code>Users</code> contains a static map that maps ids to <code>User</code> objects.
  When a <code>User</code> is initialized, I want the <code>User</code> <code>constructor</code> to check if <code>User</code> with <code>id</code> is already created, if so: return existing <code>User object</code>, else create a new <code>User object</code></p>
  <p>Example (of-course not working):</p>
  <pre><code>class Users {
    static const Map&lt;String, User&gt; 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;
   }
  }
  </code></pre>
  <p><strong>Question:</strong> IS there any way to get the above pseudo code to work?</p>