Flutter uses singletickerproviderstatemix to report an error

Time:2021-6-13

Recently, I’m learning to develop a flutter application project. After creating tabbar and tabview, I make a network request and display the top tab tag, set tabcontroller, and make the class class implement singletickerproviderstatemixin. The code is as follows

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();
  }

After running the code, the simulator does not display the tag text, and reports the following error

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.

If an error is reported, the reason is that the setstate is called in many places to request redrawing, but the state uses singletickerproviderstatemixin, just change it to tickerproviderstatemixin.
Modify the above code as follows:

class _TravelPageState extends State<TravelPage> with SingleTickerProviderStateMixin

To be amended as follows:

class _TravelPageState extends State<TravelPage> with TickerProviderStateMixin

After modification, rerun the project.

Welcome to my official account: Junwei said that the practical technical articles should be regularly promoted.
Flutter uses singletickerproviderstatemix to report an error

Recommended Today

Hot! Front and rear learning routes of GitHub target 144K

Hello, Sifu’s little friend. I’m silent Wang Er. Last week, while appreciating teacher Ruan Yifeng’s science and technology weekly, I found a powerful learning route, which has been marked with 144K on GitHub. It’s very popular. It covers not only the front-end and back-end learning routes, but also the operation and maintenance learning routes. As […]