Flutter analyzer warns: AVOID using library private types in public APIs
AVOID 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.
Fix it fast
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_api2Match what you see, then jump to its fix
- 1
The lint points at the createState line of a StatefulWidget, which reads _MyWidgetState createState() => _MyWidgetState();createState returns the private State classFix - 2
The 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
The 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
- 1
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 analyzeConfirmed by docs.flutter.dev
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_api2Fix
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 analyzeConfirmed by dart.dev
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 lints2Fix
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: false3Verify
Run the analyzer and confirm the lint is no longer listed.
flutter analyzeConfirmed by dart.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix