首页 > 其他分享 >Flutter的Invalid use of a private type in a public API警告

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

时间:2023-10-21 16:33:02浏览次数:34  
标签:use MyTabPage createState MyTabPageState private State API override public

问题描述

自己在写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<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

相关文章