0

I'm using a Dart package and need to extend or otherwise change some of the state in a singleton class called Constants, i.e. a class with a private constructor. It appears I have to change the singleton to extend/subclass it. Having poured over their code, I admire their work and want to avoid changing their code. Is their another way to add some constants and change some of the values of the static const's in their Constants._() class? I'm new enough to Dart to be missing something obvious, so don't be shy about giving me obvious advise. I know I can change the class to have a public const constructor and then I'm able subclass it. Is a mixin the way to tackle this?

Thanks in advance for any help.

  • No, there is no way to do that. This smells like an XY problem; there are perhaps other ways to solve whatever you're ultimately trying to do. What package are you talking about, how do you want to extend the class, and why? – jamesdlin Dec 03 '21 at 23:40
  • Thanks for your fast response. I was afraid this might be the case. The package is calendar_view. I need a PartialWeekView that shows only two, three or four day event calendar. Astute observation. It is an XY problem. Thanks for educating me to this kind of question asking. I'll attempt to be more direct in the future. – Mike Yinger Dec 04 '21 at 00:03
  • 2
    Back to your original question, I forgot that one way to extend such a class would be to use `implements` and then add implementations of all the overridden methods to delegate to the singleton instance. However, assuming that you're talking about the `CalendarConstants` class from [`package:calendar_view`](https://pub.dev/packages/calendar_view), that wouldn't help you anyway: `CalendarConstants` isn't a singleton. It's not meant to be instantiated anywhere; it provides only `static` getters. – jamesdlin Dec 04 '21 at 00:37
  • As for the problem that you actually want to solve: consider making a contribution to the package to add your desired feature. – jamesdlin Dec 04 '21 at 00:38
  • It's their constants.dart, Constants class. This class is a lot more complex than the CalendarConstants. I will check out 'implements'. Thanks jamesdlin. And thanks again for the XY heads-up. – Mike Yinger Dec 04 '21 at 01:07
  • That class is not any different; it's not meant to be instantiated anywhere, isn't a singleton, and provides only `static` methods that cannot be overridden. Even worse, that class is not part of the public API, so you should not be importing that file. – jamesdlin Dec 04 '21 at 01:17
  • This links to an example of ways to build a singleton in Dart. It shows the use of the ClassName._() private constructor. Doesn't that alone make it a singleton?https://stackoverflow.com/questions/12649573/how-do-you-build-a-singleton-in-dart – Mike Yinger Dec 04 '21 at 15:27
  • Singletons make use of a private constructor, but that's not the only purpose of a private constructor. The important parts to notice is that the `Constants` constructor is not invoked anywhere, and the class has only `static` members, so there is no point to creating an instance of the class. – jamesdlin Dec 04 '21 at 18:48

0 Answers0