Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Flutter analyzer warns: AVOID using library private types in public APIs

ErrorAVOID using library private types in public APIs.

Short answer

The lint fires when a public function, method, field or parameter uses a type whose name starts with an underscore. In Flutter it most often points at createState. Change the return type to State<MyWidget>, make the type public, or make the member private. If the private type is deliberate, turn the rule off for that file.

  • Applies to Flutter 3.3x, Dart 3.x, flutter_lints 2.0.0 or newer
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the analyzer and read which declaration the lint is attached to. The file and line decide which cause below applies.

    flutter analyze | grep library_private_types_in_public_api
  2. 2Match what you see, then jump to its fix

    1. 1The lint points at the createState line of a StatefulWidget, which reads _MyWidgetState createState() => _MyWidgetState();createState returns the private State classFix
    2. 2The lint points at a constructor parameter, a public field, a function return type or a type parameter that names a type starting with an underscoreA public constructor, method or field uses a private typeFix
    3. 3The warning is on code that nobody outside the package imports, or on a pattern you have decided to keepThe private type is deliberate and the rule is only adviceFix

What does the error look like?

  • AVOID using library private types in public APIs.
  • library_private_types_in_public_api

Cause 1 of 3

createState returns the private State class

1Check

createState is a public method of a public widget, and its return type names a private class, so the rule applies to it. List every widget that declares the private return type.

grep -rnE "^[[:space:]]*_[A-Za-z0-9]+State createState" lib/

2Fix

Declare the return type as the general State<MyWidget> and keep returning your private class. The private class stays private, and callers only see State<MyWidget>.

class MyWidget extends StatefulWidget {
  const MyWidget({super.key});

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  @override
  Widget build(BuildContext context) => const SizedBox();
}

3Verify

Run the analyzer again. The createState warning is gone and nothing else changed.

flutter analyze

Confirmed by docs.flutter.dev

Did this fix it?

Cause 2 of 3

A public constructor, method or field uses a private type

1Check

The rule looks at the return type of functions and methods, parameter types, type parameter bounds, the types of top-level variables and fields, and the types used in typedefs and in on clauses. Find the private type in the flagged line.

flutter analyze | grep -B1 library_private_types_in_public_api

2Fix

Either make the type public by removing the underscore from its name, or make the member that exposes it private. Pick the first when other files should use the value, the second when they should not.

// before: a public function exposes a private type
// void paint(_Brush brush) {}

// option 1: make the type public
class Brush {}
void paint(Brush brush) {}

// option 2: make the function private
// void _paint(_Brush brush) {}

3Verify

Run the analyzer. The flagged line no longer appears.

flutter analyze

Confirmed by dart.dev

Did this fix it?

Cause 3 of 3

The private type is deliberate and the rule is only advice

1Check

Check where the rule comes from. The flutter_lints package, from version 2.0.0, pulls it in through the lints package, so a project with flutter_lints in analysis_options.yaml has it on.

cat analysis_options.yaml
flutter pub deps | grep lints

2Fix

Silence it for one line or one file with an ignore comment, or switch it off for the project by setting the rule to false under linter rules. Do not mix the list form and the key-value form in the same rules entry.

// one line
// ignore: library_private_types_in_public_api
// whole file
// ignore_for_file: library_private_types_in_public_api

# analysis_options.yaml, for the whole project
linter:
  rules:
    library_private_types_in_public_api: false

3Verify

Run the analyzer and confirm the lint is no longer listed.

flutter analyze

Confirmed by dart.dev

Did this fix it?

How do I stop it coming back?

  • Write createState as State<MyWidget> createState() from the start; the Flutter cookbook examples do the same.
  • When a widget takes a private enum or class as a constructor argument, make that type public or take a public base type instead.
  • Decide once per project whether the rule stays on, and record the decision in analysis_options.yaml rather than scattering ignore comments.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.