Flutter使用SingleTickerProviderStateMixin报错解决
设置TabController
最近在学习开发Flutter应用项目,在创建tabbar和tabview后,进行网络请求后显示顶部tab标签,设置TabController,并使class类实现SingleTickerProviderStateMixin,代码如下
class _TravelPageState extends State<TravelPage> with SingleTickerProviderStateMixin{ TabController _tabController; TravelTabModel travelTabModel; List<TravelTab> tabs = []; @override void initState() { _tabController = TabController(length: 0, vsync: this); TravelTabDao.fetch().then((TravelTabModel model){ print(model); _tabController = TabController(length: model.tabs.length, vsync: this); setState(() { tabs = model.tabs; travelTabModel = model; }); }).catchError((e){ print(e); }); super.initState(); }
报错
运行代码后模拟器没有显示tab标签文字,且报如下错误
flutter: is a SingleTickerProviderStateMixin but multiple tickers were created.
A SingleTickerProviderStateMixin can only be used as a TickerProvider once.
If a State is used for multiple AnimationController objects, or if it is passed to other objects and those objects might use it more than one time in total, then instead of mixing in a SingleTickerProviderStateMixin, use a regular TickerProviderStateMixin.
分析解决
如上报错,原因是多个地方调用setState请求重绘,但是state使用的是SingleTickerProviderStateMixin ,将其改成TickerProviderStateMixin即可。
将上述代码进行修改如下:
class _TravelPageState extends State<TravelPage> with SingleTickerProviderStateMixin
修改为:
class _TravelPageState extends State<TravelPage> with TickerProviderStateMixin
修改完成后,重新运行项目即可。
以上就是Flutter使用SingleTickerProviderStateMixin报错解决的详细内容,更多关于Flutter SingleTickerProviderStateMixin的资料请关注脚本之家其它相关文章!
相关文章
Android中使用Notification实现状态栏的通知
本文主要介绍了android利用Notification实现状态栏的通知的示例代码。具有很好的参考价值。下面跟着小编一起来看下吧2017-04-0420.5 语音合成(百度2016年2月29日发布的tts引擎)
编写手机App时,有时需要使用文字转语音(Text to Speech)的功能,比如开车时阅读收到的短信、导航语音提示、界面中比较重要的信息通过语音强调2016-03-03
最新评论