Reputation: 1929
I am developing a package on Flutter.
There are I have methods in classes that are useful only for the package itself, and not for the programmer who will import my package, is possible to hide this methods in public classes for further implementation?
I am trying use @internal
annotation, but I can still see methods marked as internal outside of the package.
Example:
/// Base abstract class [ImageData]
abstract class ImageData {
/// Create [ImageData] instance.
const ImageData();
/// Create [ImageData] instance from [dto].
///
/// by converting from [dto.ImageData].
@internal
factory ImageData.fromDto(dto.ImageData object) {
switch (object.type) {
case dto.ImageData_Type.pathType:
return PathImageData(path: object.path);
case dto.ImageData_Type.rawImageType:
return RawImageData.fromDto(object.rawImage);
default:
throw const PluginInvokeException();
}
}
/// Create [dto.ImageData] from current instance.
@internal
dto.ImageData toDto();
}
and in other inheritors from this ImageData
class RawImageData extends ImageData {
/// Bytes of image.
final Uint8List data;
/// Describe type of [data] content.
final RawImageDataType type;
/// Wight of image
final int width;
/// Height of image
final int height;
/// Create [RawImageData] instance.
const RawImageData({
required this.data,
required this.type,
required this.width,
required this.height,
}) : super();
/// Create [RawImageData] instance from [dto].
///
/// by converting from [dto.RawImage].
@internal
factory RawImageData.fromDto(dto.RawImage object) {
return RawImageData(
data: Uint8List.fromList(object.raw),
type: typeFromDtoEnum(object.type),
width: object.size.width,
height: object.size.height,
);
}
@override
@internal
dto.ImageData toDto() {
return dto.ImageData(
type: dto.ImageData_Type.rawImageType,
rawImage: toDtoRawImage(),
);
}
}
But I can still see methods marked as internal outside of the package. How do private this methods only for outside package namespaces?
Upvotes: 0
Views: 2236
Reputation: 90145
Dart identifiers are either:
_
).That's it.
Annotations such as @protected
, @internal
, etc. provide additional hints to the Dart analyzer. They are not enforced at compile-time (unless you explicitly make your build system fail on analysis warnings/errors) nor at runtime. (This also is why null-safe Dart needed a new required
language keyword and could not use the old @required
annotation.)
Either accept relying on analysis warnings or rename toDto
to _toDto
to make it private. Since private identifiers are private to the Dart library, that then would entail either:
.dart
file.library
and using part
/part of
to specify the .dart
files that compose that library.Another technique for hiding package-specific identifiers is to make them public but to place them in a .dart
file internal to the package (with the expectation that clients will not explicitly import
it). That's less useful for your case, however.
Upvotes: 3