I’m looking through of sealed usage in my projects, and I can’t figure it out.
The "sealed" modifier states "The sealed modifier prevents a class from being extended or implemented outside its own library. Sealed classes are implicitly abstract." on dart.dev page.
// sign_result_entity.dart
sealed class SignResult extends Equatable {
@override
List<Object?> get props => [];
}
// sign_result_test.dart
import 'sign_result_entity.dart';
class UniqueSignResult extends SignResult {}
The problem is – I’m trying to implement sealed class outside of file, and I get the next message:
The file located in a same directory, same folder.
I believe the "library" means a different thing than we would expect.
Extending it in a same file works fine.
2
Answers
Since every Dart file is a library, you cannot extend
SignResult
from another file.The only way to do that is to use
part
/part of
.That’s why you can’t extend the class outside its library.
You can read about sealed classes in this article :
Sealed Classes in Dart: Unlocking Powerful Features
or
Dart Documentation