flutterdartoopmixinssealed

How to make mixin as sealed class in dart


How I can make mixin class "C" as sealed class cannot be extended outside its library?

class A extends B with C{}
sealed class B{}
mixin class C{}

I don't want C class to be used by a class other than "A" class


Solution

  • There are some tricky ways to accomplish this purpose:

    mark your mixin as private mixin as you know (privacy in dart is library level rather than class level) so it can't be used outside it's library.

    class A extends B with _C{}
    sealed class B{}
    mixin class _C{}
    

    another way, you can convert your mixin to sealed class which achieve your purpose also, and implement it (multiple inheritance is not allowed in dart: Diamond Problem)

    class A extends B implements C{}
    sealed class B{}
    sealed class C{}
    

    at last, class C can't be used outside the library in which it's declared, there may still other ways to accomplish your purpose. for more info see Class Modifiers Reference

    hope it helps you.