library-private-types-in-public-api

Same as library_private_types_in_public_api in Dart lints

Path
lint/correctness/library-private-types-in-public-api
Default
On (recommended)
Origin
Dart lints
Rule
254 of 512
recommended

Avoid using library-private types in public APIs.

Checks the types a public declaration exposes: type-parameter bounds, signatures, field and top-level variable types, extension on types, mixin on constraints, extension-type representations and function-type aliases. Like the analyzer, supertypes (extends, with, implements), non-function type aliases and record type annotations are not checked, and constructors of enums and of sealed, abstract final, abstract interface or @internal classes are exempt. Untyped initializing and super formals use their resolved semantic types, including declarations in part files.

Invalid

example.dartdart
class _Private {}

_Private topLevel(_Private value) => value;

_Private publicVariable = _Private();

typedef PublicFunction = _Private Function(_Private);

class PublicClass<T extends _Private> {
  _Private field;
  PublicClass(this.field);
  _Private method(_Private value) => value;
  _Private get getter => field;
  set setter(_Private value) => field = value;
}

mixin PublicMixin on _Private {
  _Private operator +(covariant _Private other) => other;
}

extension PublicExtension on _Private {}

extension type PublicExtensionType(_Private value) {}

class PublicConstructors {
  PublicConstructors(_Private value);
}
Avoid using a private type in a public API.
2
3_Private topLevel(_Private value) => value;
Avoid using a private type in a public API.
2
3_Private topLevel(_Private value) => value;
Avoid using a private type in a public API.
4
5_Private publicVariable = _Private();
Avoid using a private type in a public API.
6
7typedef PublicFunction = _Private Function(_Private);
Avoid using a private type in a public API.
6
7typedef PublicFunction = _Private Function(_Private);
Avoid using a private type in a public API.
8
9class PublicClass<T extends _Private> {
Avoid using a private type in a public API.
9class PublicClass<T extends _Private> {
10 _Private field;
Avoid using a private type in a public API.
10 _Private field;
11 PublicClass(this.field);
Avoid using a private type in a public API.
11 PublicClass(this.field);
12 _Private method(_Private value) => value;
Avoid using a private type in a public API.
11 PublicClass(this.field);
12 _Private method(_Private value) => value;
Avoid using a private type in a public API.
12 _Private method(_Private value) => value;
13 _Private get getter => field;
Avoid using a private type in a public API.
13 _Private get getter => field;
14 set setter(_Private value) => field = value;
Avoid using a private type in a public API.
16
17mixin PublicMixin on _Private {
Avoid using a private type in a public API.
17mixin PublicMixin on _Private {
18 _Private operator +(covariant _Private other) => other;
Avoid using a private type in a public API.
17mixin PublicMixin on _Private {
18 _Private operator +(covariant _Private other) => other;
Avoid using a private type in a public API.
20
21extension PublicExtension on _Private {}
Avoid using a private type in a public API.
22
23extension type PublicExtensionType(_Private value) {}
Avoid using a private type in a public API.
25class PublicConstructors {
26 PublicConstructors(_Private value);

Valid

example.dartdart
class _Private {
  _Private field;
  _Private(this.field);
  _Private method(_Private value) => value;
}

_Private _privateFunction(_Private value) => value;
_Private _privateVariable = _Private(_Private(_Private(null as dynamic)));
typedef _PrivateAlias = _Private Function(_Private);

class PublicClass {
  String field;
  PublicClass(this.field);
  String method(String value) => value;
  _Private _privateMethod(_Private value) => value;
}

typedef PublicAlias = ({String value, void Function(String) callback});
extension PublicExtension on String {}
extension on _Private {}
extension type PublicExtensionType._(_Private _value) {}

enum PublicEnum {
  value._(null);
  const PublicEnum._(_Private? value);
}

void publicNamedParameter({String? value}) {}
void _privateNamedParameter({_Private? value}) {}

// Supertypes are not public API to the analyzer.
class _Base {}
mixin _Mixin {}
abstract class _Marker {}
class PublicSubclass extends _Base with _Mixin implements _Marker {}

// Non-function aliases and record types are not checked.
typedef PublicRecordAlias = ({_Private value, void Function(_Private) callback});
typedef PublicClassAlias = _Private;
({_Private value}) publicRecord() => (value: _Private(null as dynamic));

// Constructors of effectively private classes are exempt.
sealed class PublicSealed {
  PublicSealed(_Private value);
}
abstract final class PublicAbstractFinal {
  PublicAbstractFinal(_Private value);
}
abstract interface class PublicAbstractInterface {
  PublicAbstractInterface(_Private value);
}

Configure library-private-types-in-public-api

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "library-private-types-in-public-api": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/library-private-types-in-public-api: <reason>
  • list-all-equatable-fieldsrecommendedequatable

    Flags declared instance fields omitted from a statically understood Equatable props list.

  • literal-only-boolean-expressionsliteral_only_boolean_expressionsrecommended

    Flags conditions built only from literals, whose value never changes.

  • match-getter-setter-field-names

    Detect likely copy-paste errors in trivial accessors when a different field matching the accessor name is declared.

  • missing-test-assertionrecommendedtest

    Reports inline test callbacks that contain no recognized assertion calls.

  • no-adjacent-strings-in-listno_adjacent_strings_in_listrecommended

    Flags adjacent string literals used as an element of a list or set literal.

  • no-import-cyclescross-file

    Report mutually dependent components of imported or exported library files.

  • no-logic-in-create-stateno_logic_in_create_staterecommendedflutter

    Require StatefulWidget.createState to return only a fresh zero-argument Flutter State construction.

  • no-self-assignmentsno_self_assignmentsrecommended

    Flags assignments of a variable to itself, such as x = x;.