Flutter singleton separate approach

Singleton

class SampleSingle {
  SampleSingle._private();
  static final instance = SampleSingle._private();

  // Usage:
  // SampleSingle.instance
}

Normal class

class SampleNormal {
  SampleNormal();

// Usage:
// SampleNormal()
}

Avoid use factory to override default constructor, the source can be not transparent.

Here the approach singleton using Factory

class Singleton {
  static final Singleton instance = Singleton._private();
  Singleton._private();
  factory Singleton() => instance;
}

Leave a Reply

Your email address will not be published.Required fields are marked *