Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any sealed classes alternatives in Dart 2.0?

Tags:

flutter

dart

I have Android development background and I'm learning Flutter.

In Android it's a common practice to use Kotlin sealed classes to return a state from ViewModel e.g.

sealed class MyState {
    data class Success(val data: List<MyObject>) : MyState()
    data class Error(val error: String) : MyState()
}

I want to use similar pattern in Flutter and return a State object from the BLOC class. What is the best way to achieve the same in Flutter?

like image 380
Vadims Savjolovs Avatar asked Jan 22 '19 21:01

Vadims Savjolovs


People also ask

What is sealed class in Dart?

Kotlin sealed classes guarantee that there are no other implementations of the given class outside of the file they're defined in. That means you can exhaust when statements ( switch in Dart) by just providing all possible alternatives as cases, not having to think about potential sub-classes elsewhere.

What are sealed classes?

Sealed classes and interfaces represent restricted class hierarchies that provide more control over inheritance. All direct subclasses of a sealed class are known at compile time. No other subclasses may appear outside a module within which the sealed class is defined.

What is sealed class in Kotlin?

Here, we have a data class success object which contains the success Data and a data class Error object which contains the state of the error. This way, Sealed classes help us to write clean and concise code! That's all the information about Sealed classes and their usage in Kotlin.


2 Answers

Such use case would be done using named factory constructors.

It requires a lot more code, but the behavior is the same.

class MyState {   MyState._();    factory MyState.success(String foo) = MySuccessState;   factory MyState.error(String foo) = MyErrorState; }  class MyErrorState extends MyState {   MyErrorState(this.msg): super._();    final String msg; }  class MySuccessState extends MyState {   MySuccessState(this.value): super._();    final String value; } 
like image 56
Rémi Rousselet Avatar answered Sep 19 '22 01:09

Rémi Rousselet


Rémi Rousselet's answer is somehow correct but as sindrenm mentioned:

Unfortunately, this isn't the same thing. Kotlin sealed classes guarantee that there are no other implementations of the given class outside of the file they're defined in. That means you can exhaust when statements (switch in Dart) by just providing all possible alternatives as cases, not having to think about potential sub-classes elsewhere

While there is an active discussion about this feature on dart language: Algebraic Data Types, but there is some libraries that can help you implement this behavior. You can use this libraries:

Sealed Unions

Super Enum

Sealed Class

And if you are using BLoC library you can use this lib:

Sealed Flutter Bloc

I hope that dart language add this feature ASAP

like image 36
Saman Sattari Avatar answered Sep 18 '22 01:09

Saman Sattari