Dart包-如何隐藏内部方法和类? [英] Dart Package - How to hide internal methods and classes?

查看:42
本文介绍了Dart包-如何隐藏内部方法和类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Flutter应用开发程序包

I am developing a package for Flutter Apps

有些方法和类仅对包本身有用,而对要导入我的包的程序员不有用,是否可以隐藏这些方法和类以进一步实现?

There are methods and 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 and classes for further implementation?

示例:

DataService.dart

DataService.dart

export class DataService{

    //Must be visible only for my library
    static notifyDataChanged(InternalEvent internalEvent){ ... }

    //Must be visible for anyone
    static addCallbackOnDataChange(onDataChangeCallback) { ... }

}

InternalEvent.dart

InternalEvent.dart

//Must be visible only for my library as well
export class InternalEvent {
   ...
}

推荐答案

仅具有包声明的通常方法是将它们放在 lib/src/目录中的库中,而不是导出该库.软件包中的其他库可以 import 仅软件包的库,但是不鼓励软件包外部的用户直接导入 lib/src/中的库.(这并不是不可能的,只是有些令人沮丧的事情,因为该软件包可以自由更改这些库而不会发出警告).

The usual approach to having package-only declarations is to put them in a library in the lib/src/ directory, and not export that library. The other libraries in the package can import the package-only library, but users outside the package are discouraged from importing libraries in lib/src/ directly. (It's not impossible, just something that's discouraged because the package is free to change those libraries without warning).

如果仅打包功能需要访问公共类的库私有部分,则它们必须位于同一库中.然后,传统的方法是在 lib/src/的库中声明两者,并仅导出该库中需要公开的部分:

If the package-only features require access to library private parts of public classes, then they need to be in the same library. The traditional way is then to declare both in a library in lib/src/ and export only the parts of that library which needs to be public:

library myPackage;
export "src/allDeclarations" hide Private, Declarations;
// or 
export "src/allDeclarations" show Public, Things;

通常,只有在绝对必要时,才应将导出的声明和未导出的声明放在同一库中.否则, hide / show 列表会变得过于繁琐,并且很容易忘记 hide 列表中的声明.

Generally you should only put exported and non-exported declarations in the same library if absolutely necessary. Otherwise the hide/show lists become too cumbersome and it's to easy to forget a declaration in a hide list.

这篇关于Dart包-如何隐藏内部方法和类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆