flutterdartundefined

The method 'createTicker' isn't defined for the type


I was trying to create a simple ticker:

Ticker? ticker;

@override
void initState() {
  super.initState();
  ticker = createTicker(tick);
  ticker!.start();
}

@override
void dispose() {
  ticker?.stop();
  ticker?.dispose();
  super.dispose();
}

void tick(Duration elapsed) {
  setState(() {
    // Do whatever
  });
}

The problem is that I get the error The method 'createTicker' isn't defined for the type '[WidgetName]'. I can't find any info about this. Is depracated or something? I can't find any other method like this. And yes, I am importing 'package:flutter/scheduler.dart'


Solution

  • You should use SingleTickerProviderStateMixin. Later, in case you need more than 1 ticker object, you may consider migrate to TickerProviderStateMixin.

    These mixins declare needed method createTicker.

    So, just add mixing to you state like this:

    class _MyWidgetState extends State<MyWidget>
        with SingleTickerProviderStateMixin { ... }