flutterdartsealed-class

I want to know the definition of "sealed class" in Dart


https://www.youtube.com/watch?v=2Cl0C-9dK48&list=PLjxrf2q8roU1fRV40Ec8200rX6OuQkmnl

Type Promotion | Decoding Flutter

In the above video, there is the following explanation.

Dart doesn't have sealed classes. That means every class can be extended or even implemented.

I sometimes see the term "sealed classes", but I can't find a definition for this term in Dart. Is there any documents or something that is clearly defined in Dart?

https://dart.dev/guides/language/language-tour#enumerated-types

Note: All enums automatically extend the Enum class. They are also sealed, meaning they cannot be subclassed, implemented, mixed in, or otherwise explicitly instantiated.

I found the above sentence when I searched, but is this the definition of "sealed classes" after all?


Solution

  • This link explains the sealed class in dart.

    // UnitedKingdom --+-- NorthernIreland
    //                 |
    //                 +-- GreatBritain --+-- England
    //                                    |
    //                                    +-- Scotland
    //                                    |
    //                                    +-- Wales
    sealed class UnitedKingdom {}
    class NorthernIreland extends UnitedKingdom {}
    sealed class GreatBritain extends UnitedKingdom {}
    class England extends GreatBritain {}
    class Scotland extends GreatBritain {}
    class Wales extends GreatBritain {}
    

    Marking not just UnitedKingdom sealed, but also GreatBritain means that all of these switches are exhaustive:

    test1(UnitedKingdom uk) {
      switch (uk) {
        case NorthernIreland(): print('Northern Ireland');
        case GreatBritain(): print('Great Britain');
      }
    }
    

    As of today, the status is in Progress.

    EDIT: The above link is now moved to a new location, and the current status is Accepted.

    Update: Thanks, @Abion47, The sealed classes are now merged and here's the GitHub link for that ticket.