将 linter 升级到新版本后(flutter_lints: 2.0.1
)在我的 pubspec 中
linter 启用此规则:公共 API 中的库私有类型 https://dart.dev/tools/linter-rules#library_private_types_in_public_api默认情况下。我不明白为什么。在我的应用程序项目中,有很多由以下扩展的小部件类StatefulWidget
。国家级始终是私有的。Example https://api.flutter.dev/flutter/widgets/StatefulWidget-class.html项目也以同样的方式进行。有人可以解释为什么这条规则有意义吗?
顺便说一句:我知道我可以禁用此规则。
我遇到了同样的问题,看来现在当你生成一个StatefulWidget
而不是返回说_ExampleState
in the createState
它现在返回的方法State<Example>
这可以避免返回私有类型。我最终将所有小部件更新为这种方法。
so
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();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)