Getting Started

First of all, you have to switch from Flutter stable to dev due it is only available in dev at the moment. After that just open pubspec.json and add meta: 1.1.7

dependencies:

flutter:

sdk: flutter

meta: ^1.1.7

And that’s it, you are ready to go.

Ok, what it can and what can’t?

Let’s create or own first sealed class that represents Result

import 'package:meta/meta.dart'; @sealed

class Result<T> {}



class Success<T> extends Result<T> {

final T value;



Success(this.value);

}



class Error<T> extends Result<T> {

final Exception exception;



Error(this.exception);

}

Looks easy, isn’t it? But the issues start appearing, in comparison with Kotlin you can see that Success and Error aren’t inner classes of Result.

But what if we tried to create a child inside another package? Would it work?

Ouch, yes, it would ¯\_(ツ)_/¯:

class AnotherSuccessInsideAnotherPackage extends Result<String> {

String askWhy() {

return "Why Dart, WHYYY?";

}

}



tryUsing() {

final Result<String> result = AnotherSuccessInsideAnotherPackage();



(result as AnotherSuccessInsideAnotherPackage).askWhy();

}

And everything is explained in the doc of @sealed annotation

/// Annotation marking a class as not allowed as a super-type.

///

/// Classes in the same package as the marked class may extend, implement or

/// mix-in the annotated class.

///

/// Tools, such as the analyzer, can provide feedback if

///

/// * the annotation is associated with anything other than a class,

/// * the annotation is associated with a class `C`, and there is a class or

/// mixin `D`, which extends, implements, mixes in, or constrains to `C`, and

/// `C` and `D` are declared in different packages.

const _Sealed sealed = const _Sealed();

Ah ok, so it only works if we try to use it from another package (thanks for pointing this out @mraleph) And indeed if we try to inherit Result from another package, IDE will warn us about it

Also, you can run flutter analyze through CLI