I'm following a book that uses a factory method to implement a class that is a singleton.
I understand that the main purpose of this is to have just one instance of the class; But what exactly the keyword "factory" does in flutter?
This is the piece of code that I'm referring:
static final DbHelper _dbHelper = DbHelper._internal();
DbHelper._internal();
factory DbHelper() => _dbHelper;
I supose that the _dbHelper
is the single instance that is being create with the _internal
named constructor
and that the factory method returns this single instance, is that correct? Am I missing something?
You have it correct!
You can read more about factory constructors here: https://dart.dev/language/constructors#factory-constructors