Because SharedPreferences, by its very nature, is singleton (i.e. There’s only one ‘preferences’ per app) indeed the singleton approach is being used in this library. It’s just all in the delivery. True, the use of a singleton factory class would be just as effective. Just not as pretty.

Any developer experienced with this library can simply import it to their Dart file, prefix the ol’ ‘Prefs’ in front of whatever library function they wish to use in their particular circumstance, and they’re off and running!

Image for post
Image for post

Being all static functions, they be can called anytime; anywhere without having to call a factory class first and assign to a variable, etc. Done! That developer then gets on with their life. Another developer, possibly just as familiar with the library, can later come across the code and can already recognize what’s going on…at least in that regard. Self-contained. Clean.

Greg

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store