12

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();
  }
}
Ankit Kumar
  • 209
  • 4
  • 10
  • Does this answer your question? [library\_private\_types\_in\_public\_api and StatefulWidget](https://stackoverflow.com/questions/72233178/library-private-types-in-public-api-and-statefulwidget) – mmcdon20 Jun 19 '22 at 16:17

7 Answers7

28

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();
  }
}
abdullah_bd
  • 484
  • 3
  • 12
Harish Sharma
  • 1,189
  • 1
  • 8
  • 19
4

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.

kaine119
  • 41
  • 2
2

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();
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
0

this would be the solution:

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

@override State createState() => _MyPageState(); }

0

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),
   ),
  );
 }
}
Ven Ren
  • 1,584
  • 1
  • 13
  • 24
-1

Just change _SubCategoriesPage by State

For those using Riverpod, change _SubCategoriesPage by ConsumerState

Jack'
  • 1,722
  • 1
  • 19
  • 27
-1

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.

David S
  • 45
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 08 '22 at 15:58
  • So what was the answer to the above comments? – David S Dec 24 '22 at 20:22