library_private_types_in_public_api 和 StatefulWidget

library_private_types_in_public_api and StatefulWidget

在我的 pubspec 中将 linter 升级到新版本 (flutter_lints: 2.0.1) 之后 linter 启用此规则: library_private_types_in_public_api by default. I don't understand why. In my App project there are a lot of widget classes extended by StatefulWidget. The state class is always private. Example 个项目也是如此。有人可以解释为什么这条规则有意义吗?

顺便说一句:我知道我可以禁用此规则。

我遇到了同样的问题,似乎现在当你生成一个 StatefulWidget 而不是在 createState 方法中返回 _ExampleState 时,它现在 returns State<Example> 避免返回私有类型。我最终将我所有的小部件更新为这种方法。

所以

class Example extends StatefulWidget {
  const Example({Key? key}) : super(key: key);

  @override
  _ExampleState createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

可以改写为

class Example extends StatefulWidget {
  // you can also now use a super initializer for key 
  // if you are using dart 2.17
  const Example({super.key});

  // now returning State<Example>
  @override
  State<Example> createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}