Thursday, June 20, 2024
Google search engine
HomeDevelopersExplore Sealed Classes In Dart

Explore Sealed Classes In Dart

In this article, we will Explore Sealed Classes In Flutter. We perceive how to execute a demo program. We will show you what is a sealed classes? and how to use it in your applications.

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.


Table Of Contents::

What Is Sealed Classes?

Subclasses

Instances

Constructors

Switch Cases Expression Checking

Conclusion


What Is Sealed Classes?:

Sealed is a new modifier that was added in Dart 3. It can be applied to define a class or type with a restricted list of subtypes. We would like to create a class called Color, for instance. The Green, Blue, and Red classes are the only three that can be subtypes of the Color class.

Adding a sealed modifier to the Color class is the answer. The class cannot be expanded upon or used outside of its library with that modifier applied. Sealed classes are also inherently abstract.

Subclasses:

Every subclass needs to be defined in the same file, or the same library. An example of declaring the Color class and its subclasses is provided below.

sealed class Color {}

class Green extends Color {}

class Blue extends Color {}

class Red extends Color {
final String name;

Red(this.name);
}

The compiler will raise an error if you attempt to define a class in another file that extends the Color class.

class Item extends Color {}

Instances:

A sealed class’s constructor cannot be used to create an instance since it is implicitly abstract.

Color color = Color();

A sealed class’s subclasses are not inherently abstract. As a result, to create instances, you must use the constructor of the subclasses.

Green myGreen = Green();
Blue myBlue = Blue();
Red myRed = Red('flutterdevs.com');

Constructors:

Constructors defined by a sealed class are accessible to its subclasses. For instance, we add a field called id to the Color class mentioned above. A constructor exists that takes in the value of id. I included a print statement to make it simpler to determine whether the constructor is called when from the subclasses.

sealed class Color {

String id;

Color(this.id) {
print('Creating a color');
}
}

class Green extends Color {
Green(super.id);
}

class Blue extends Color {
Blue(super.id);
}

class Red extends Color {
final String name;

Red(String id, this.name) : super(id) {
print('Creating a red');
}
}

The ‘Creating a color’ text should appear when you attempt to create an instance of the subclass using the code mentioned above.

Additionally, some factory constructors can be defined.

sealed class Color {

String id;

Color(this.id) {
print('Creating a color');
}

factory Color.createByType(String type, String id) {
if (type == "green") {
return Green(id);
} else if (type == "blue") {
return Blue(id);
} else if (type == "red") {
return Red(id, 'flutterdevs.com');
}

throw UnsupportedError('Unknown type');
}
}

Switch Cases Expression Checking:

To determine whether an object is an instance of a specific class, you can construct a switch case in the Dart programming language. The compiler can notify you if a switch block isn’t handling every possible subtype because a sealed class has a known list of subtypes.

The switch block in the example below doesn’t handle the case in which the passed object is Red. Consequently, an error stating that the switch cases do not fully match the type will be displayed.

String getColorVoice(Color color) {
// ERROR: The type 'Color' is not exhaustively matched by the switch cases since it doesn't match 'Goat()'
return switch (color) {
Green() => 'light',
Blue() => 'dark',
};
}

Conclusion:

In the article, I have explained the Sealed Classes In Dart; you can modify this code according to your choice. This was a small introduction to Sealed Classes In Dart User Interaction from my side, and it’s working using Flutter.

I hope this blog will provide you with sufficient information on Trying the Sealed Classes In Dart of your projects. If you want to define a class whose list of subtypes is predetermined and cannot be altered later, you should use Dart’s sealed modifier. The same library (file) must declare the list of subtypes.

Classes that are sealed cannot be instantiated and are implicitly abstract. You can, however, include factory constructors and other constructors. When you construct a switch block with a sealed class as the checked object type, the compiler can determine whether the switch cases have already exhaustively matched the type.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on Facebook, GitHub, Twitter, and LinkedIn.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments