Reputation: 399
I have been working on a flutter project and I have noticed Avoid using private types in public APIs. Is there a way to fix this warning?
class SubCategoriesPage extends StatefulWidget {
final MainModel mainModel;
// final Ads ad;
const SubCategoriesPage(this.mainModel, {Key? key}) : super(key: key);
@override
_SubCategoriesPage createState() { // Avoid using private types in public APIs.
return _SubCategoriesPage();
}
}
Upvotes: 29
Views: 16752
Reputation: 681
There is a better and simple solution.
Either, for all the files in your projet:
Modify in analysis_options.yaml at the root of your project
rules:
library_private_types_in_public_api: false
Either, for a single file in your projet:
add the comment
// ignore: library_private_types_in_public_api
above the line with problem (although they says it can be at the start of the file, it doesn't work for me.
Hope this help.
Upvotes: 0
Reputation: 1549
Because createState
method return State<Example>
so it's preventing returning any private State
.
You need to update your code like this.
class SubCategoriesPage extends StatefulWidget {
final MainModel mainModel;
// final Ads ad;
const SubCategoriesPage(this.mainModel, {super.key});
@override
State<SubCategoriesPage> createState() { // Avoid using private types in public APIs.
return _SubCategoriesPage();
}
}
Upvotes: 56
Reputation: 3358
In my case, I remove '_MyHomePageState'
class MyHomePage extends StatefulWidget {
const MyHomePage({super. Key, required this. Title});
final String title;
@override //Here remove _MyHomePageState
createState() => _MyHomePageState();
}
The code in _HomePageState class
class _HomePageState extends State<HomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget. Title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('You have pushed the button this many times:'),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter, //click
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
Upvotes: 0
Reputation: 21
this would be the solution:
class MyPage extends StatefulWidget { const MyPage({super.key});
@override State createState() => _MyPageState(); }
Upvotes: 0
Reputation: 161
I had the same problem
class CotizacionScreen extends StatefulWidget {
const CotizacionScreen({super.key});
@override
State<CotizacionScreen> createState() => _CotizacionScreenState();
}
I solved it by changing the:
@override
_CotizacionScreenState createState() => _CotizacionScreenState();
by:
@override
State<CotizacionScreen> createState() => _CotizacionScreenState();
Upvotes: 15
Reputation: 45
I had the same issue using this code
class MyPage extends StatefulWidget {
const MyPage({super.key});
@override
_MyPageState createState() => _MyPageState();
}
I tried using this method - State createState() { // Avoid using private types in public APIs. return _MyPageState(); but than I got this error message - The name MyPageState isn't a type so it can't be used a type argument.
Upvotes: -1
Reputation: 2508
Just change _SubCategoriesPage
by State
For those using Riverpod, change _SubCategoriesPage
by ConsumerState
Upvotes: 0
Reputation: 51
Since this is a StatefulWidget
, I'm guessing the _SubCategoriesPage
class inherits from State
, since it's being returned by createState()
.
If so, the return type can be changed to State
. Since State
is public, it can safely be returned from the public createState()
method.
Upvotes: 5