Home > Net >  "Avoid using private types in public APIs" Warning in Flutter
"Avoid using private types in public APIs" Warning in Flutter

Time:06-20

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();
  }
}

CodePudding user response:

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.

CodePudding user response:

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, {Key? key}) : super(key: key);

  @override
  State<SubCategoriesPage> createState() { // Avoid using private types in public APIs.
    return _SubCategoriesPage();
  }
}
  • Related