我正在开发一个关于 Flutter 的包。
我在类中有一些方法仅对包本身有用,对导入我的包的程序员没有用,是否可以在公共类中隐藏这些方法以进一步实现?
我正在尝试使用@internal
注释,但我仍然可以看到标记为包外部内部的方法。
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();
}
以及其他继承者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(),
);
}
}
但我仍然可以看到标记为包外部内部的方法。如何私有化这个方法仅供室外使用包命名空间?
Dart 标识符是:
就是这样。
注释如@protected
, @internal
等为 Dart 分析器提供了额外的提示。它们不会在编译时(除非您明确地使构建系统因分析警告/错误而失败)或运行时强制执行。 (这也是为什么空安全 Dart 需要一个新的required
language 关键字,无法使用旧的@required
注解。)
接受依赖分析警告或重命名toDto
to _toDto
将其设为私有。由于私有标识符对于 Dart 来说是私有的library,那么这将需要:
- 将所有代码移至同一个
.dart
file.
- 显式声明 Dart 库
library
并使用part
/part of
指定.dart
组成该库的文件。
隐藏特定于包的标识符的另一种技术是将它们公开,但将它们放在.dart
包内部的文件(期望客户端不会明确地import
它)。但是,这对您的情况不太有用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)