Flutter的Invalid use of a private type in a public API警告

发布于:2023-10-25 ⋅ 阅读:(114) ⋅ 点赞:(0)

问题描述

自己在写Flutter 应用时发现了一个Invalid use of a private type in a public API警告。

发现很多官方的例子也有这个问题。
image.png

有问题的源码

有问题的源码如下:

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 createState() => _SomeWidgetState();

解决方法

_MyTabPageState createState() => _MyTabPageState();

改为:

State<MyTabPage> createState() => _MyTabPageState();

修改后的代码

class MyTabPage extends StatefulWidget {
  const MyTabPage({super.key});
  @override
  State<MyTabPage> createState() => _MyTabPageState();
}

网站公告

今日签到

点亮在社区的每一天
去签到