Dart Lint错误library_private_types_in_public_api修改后仍报错求助
解决library_private_types_in_public_api Lint错误
你之前的修改写法存在语法错误,<State>ActiveEstimateListScreen不符合Dart泛型规范,所以无法解决问题。要修复这个lint错误,核心是避免在公共API中直接暴露私有状态类_ActiveEstimateListScreenState,正确的处理方式如下:
正确的createState写法
@override State<ActiveEstimateListScreen> createState() => _ActiveEstimateListScreenState(estimates: estimates);
关键注意事项
- 必须添加
@override注解:createState是StatefulWidget的重写方法,缺少注解可能引发额外lint问题 - 明确指定返回类型为
State<ActiveEstimateListScreen>:通过泛型声明返回对应Widget的State,而非直接返回私有状态类,避免在公共API中暴露私有类型 - 确保状态类继承正确:私有状态类需要正确继承自
State<ActiveEstimateListScreen>,示例如下:
class _ActiveEstimateListScreenState extends State<ActiveEstimateListScreen> { final List<Estimate> estimates; _ActiveEstimateListScreenState({required this.estimates}); // 其他状态逻辑代码 }
如果修改后仍有错误,建议排查:
- 项目中是否有其他公共方法(参数、返回值)直接使用了
_ActiveEstimateListScreenState私有类型 - 确认
ActiveEstimateListScreen是正确的StatefulWidget子类
内容的提问来源于stack exchange,提问作者Aimee Bogle
相关产品推荐
相关产品推荐

