class Logger {
factory Logger(String name) => Logger._internal(name);
Logger._internal(this.name);
final String name;
}
Does this way of writing hold as a so-called singleton pattern?
class Logger {
factory Logger(String name) => Logger._internal(name);
Logger._internal(this.name);
final String name;
}
Does this way of writing hold as a so-called singleton pattern?
2
Answers
You should not be making the singleton variables as final cause your goal of making the singleton is to have the same value shared between different class and can be changed from anywhere.
If your singleton variables are not getting changed than why they are in the singleton, They should be in the constants.
It should be like this:
And will be used like these, with making a instance or directly with the name:
Here if you don’t want to give the value first time before using can just give it at the declaration in the singleton itself that way it don’t need to be nullable.
I use Riverpod Provider objects as lazy singletons, because they are, well, lazy, and they also can be overridden for testing… something difficult to do from a traditional singleton pattern.