Benjamin
Benjamin

Reputation: 6161

Lazy Singleton vs Singleton in Dart

I'm using the get_it package and you get two options for registering Singletons, lazy and I guess "regular" (GetIt.instance.registerLazySingleton and GetIt.instance.registerSingleton respectively) Here's one of the classes that's registered as a plain Singleton:

class AndroidDetails {
  static final DeviceInfoPlugin _deviceInfoPlugin = DeviceInfoPlugin();
  Map<String, dynamic> _deviceData = {};

  AndroidDetails() {
    _init().then((_) => getIt.signalReady(this));
  }

  Future<void> _init() async {
    try {
      AndroidDeviceInfo _deviceInfo = await deviceInfoPlugin.androidInfo;
      _deviceData = _getDeviceData(_deviceInfo);
    } on PlatformException {
      _deviceData = <String, dynamic>{
        'Error:': 'Failed to get platform version.',
      };
    }
  }

  Map<String, dynamic> _getDeviceData(AndroidDeviceInfo build) {
    return <String, dynamic>{
      'version.sdkInt': build.version.sdkInt,
    };
  }

  bool canChangeStatusBarColor() {
    if (_deviceData.isNotEmpty) {
      return _deviceData['version.sdkInt'] >= 21;
    }
    return null;
  }

  bool canChangeNavbarIconColor() {
    if (_deviceData.isNotEmpty) {
      return _deviceData['version.sdkInt'] >= 27;
    }
    return null;
  }
}

How it's registered:

// main.dart
getIt.registerSingleton<AndroidDetails>(AndroidDetails(), signalsReady: true);

My question is, what's the difference between a "normal" Singleton and a Lazy Singleton in Dart & the get_it package?

Upvotes: 19

Views: 16374

Answers (4)

Yuriy N.
Yuriy N.

Reputation: 6077

I would dare to say that all the above answers are not correct enough.

We cannot have eager (the anthonym of lazy) Singletons in Dart. Any Singleton (pattern) implementation we can think about will be lazy, since Dart has a lazy loading feature for global and static variables.

GetIt is completely different story.

GetIt has two methods registerSingleton and registerLazySingleton. The first takes the instance of the object as an argument, but the second takes a builder function which called when object is needed the first time.

SL.registerSingleton<ProductServiceInterface>(ProductService());

SL.registerLazySingleton<ProductServiceInterface>(() => ProductService());

While practically GetIt allows to have a single instance per class the implementation is not typical Singleton pattern implementation.

Practically, we should use registerLazySingleton most of the time since in a mobile app we want the start up time to be as short as possible.

Worth to note, that GetIt also has registerAsync methods that can be handy in some situations.

Upvotes: 0

Narendra Shekhawat
Narendra Shekhawat

Reputation: 1

Singletons and LazySingleton both refer to a class that is first initialised before using it for the first time. And after that, it can be used globally to access it. It's mostly used to save memory and resources in the project.

Upvotes: 0

Guru Prasad mohapatra
Guru Prasad mohapatra

Reputation: 1969

Both are Singletons. But LazySingleton refers to a class whose resource will not be initialised until its used for the 1st time. It's generally used to save resources and memory.

Upvotes: 35

Denis G
Denis G

Reputation: 571

"Lazy" refers to initiating resources at the time of the first request instead at the time of declaration. More reading on the concept is here: https://en.wikipedia.org/wiki/Lazy_initialization

Upvotes: 6

Related Questions