码迷,mamicode.com
首页 > 其他好文 > 详细

Flutter使用SingleTickerProviderStateMixin报错

时间:2020-01-16 14:41:15      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:错误   obj   mod   extends   can   time   xtend   instead   网络   

最近在学习开发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报错

标签:错误   obj   mod   extends   can   time   xtend   instead   网络   

原文地址:https://www.cnblogs.com/wayne6688/p/12200640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!