1

I am using flutter_easyLoading package for loaders in my flutter project. It says in the documentation that it creates a singleton and I have to define its properties only once somewhere and it would be available throughout the app. What is the best practice to define these properties?

Right now I am initializing its variables in a splash screen file like this.

class _SplashScreenState extends State<SplashScreen> {
  @override
  void didChangeDependencies() async {
    super.didChangeDependencies();
  EasyLoading.instance
    ..displayDuration = const Duration(milliseconds: 2000)
    ..indicatorType = EasyLoadingIndicatorType.fadingCircle
    ..loadingStyle = EasyLoadingStyle.dark;
 }

Should I do it this way or maybe define some util method for all these properties.

tensor
  • 733
  • 1
  • 13
  • 22
  • Does this answer your question? [How do you build a Singleton in Dart?](https://stackoverflow.com/questions/12649573/how-do-you-build-a-singleton-in-dart) – Md. Yeasin Sheikh Dec 31 '21 at 13:57
  • No, this question is about how to construct a singleton. I already have a singleton and I need a way to initialize it with given properties as explained in package documentation at the end. – tensor Dec 31 '21 at 14:02
  • `What is the best practice...` Then it might go under opinion based question. Can you include your code snippet and issue you are facing. – Md. Yeasin Sheikh Dec 31 '21 at 14:18

1 Answers1

0

You can use a factory constructor implement singleton classes in dart.

This is a simple example adapted to this context

class EasyLoadingSingleton {
  static final EasyLoadingSingleton _easyloading = EasyLoadingSingleton._internal();

  factory EasyLoadingSingleton() {
    return _easyloading;
  }

  EasyLoadingSingleton._internal();
}
Baimam Boukar
  • 908
  • 5
  • 20