Flutter使用SingleTickerProviderStateMixin报错

简介: Flutter使用SingleTickerProviderStateMixin报错

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

修改完成后,重新运行项目即可。

目录
相关文章
|
3月前
|
缓存 Dart 开发工具
解决Flutter报错The method ‘File.create‘ has fewer named arguments than those of overridden method
解决Flutter报错The method ‘File.create‘ has fewer named arguments than those of overridden method
60 3
|
11天前
|
存储 测试技术 Shell
Flutter UT太多导致跑覆盖率报错
Flutter UT太多导致跑覆盖率报错
23 2
|
3月前
Flutter更改主题颜色报错:type ‘Color‘ is not a subtype of type ‘MaterialColor‘
Flutter更改主题颜色报错:type ‘Color‘ is not a subtype of type ‘MaterialColor‘
44 4
|
3月前
|
开发工具 iOS开发
解决Flutter运行报错Could not run build/ios/iphoneos/Runner.app
解决Flutter运行报错Could not run build/ios/iphoneos/Runner.app
141 2
|
3月前
解决Flutter报错The named parameter |method ‘xxxx‘ isn‘t defined.
解决Flutter报错The named parameter |method ‘xxxx‘ isn‘t defined.
127 3
|
3月前
|
Dart
Flutter使用Scaffold报错。
Flutter使用Scaffold报错。
44 3
|
3月前
|
iOS开发
解决Flutter运行IOS报错:Podfile is out of date
解决Flutter运行IOS报错:Podfile is out of date
74 1
|
3月前
解决Flutter报错boxconstraints has non-normalized height/width constraints
解决Flutter报错boxconstraints has non-normalized height/width constraints
44 0
|
3月前
|
Android开发
解决Android、Flutter编译时Gradle报错:javax.net.ssl.SSLException: Connection reset
解决Android、Flutter编译时Gradle报错:javax.net.ssl.SSLException: Connection reset
355 0
|
API Android开发
Flutter导入第三方包后报错The number of method references in a .dex file cannot exceed 64K
Flutter导入第三方包后报错The number of method references in a .dex file cannot exceed 64K