问题描述
自己在写Flutter 应用时发现了一个Invalid use of a private type in a public API警告。
发现很多官方的例子也有这个问题。
有问题的源码
有问题的源码如下:
class MyTabPage extends StatefulWidget {
const MyTabPage({super.key});
@override
_MyTabPageState createState() => _MyTabPageState();
}
问题原因
在公共API中使用私有类型无效。
Creates the mutable state for this widget at a given location in the tree. Subclasses should override this method to return a newly created instance of their associated State subclass: @override State<SomeWidget> createState() => _SomeWidgetState();
解决方法
将
_MyTabPageState createState() => _MyTabPageState();
改为:
State<MyTabPage> createState() => _MyTabPageState();
修改后的代码
class MyTabPage extends StatefulWidget {
const MyTabPage({super.key});
@override
State<MyTabPage> createState() => _MyTabPageState();
}
标签:use,MyTabPage,createState,MyTabPageState,private,State,API,override,public
From: https://blog.51cto.com/u_15777557/7968031