2

I am playing with flutter and trying to measure performance. My intention is to change state as often as possible and check how this framework is going to deal with it. I use WidgetsBinding.instance.addTimingsCallback to measure timing.

class _MyHomePageState extends State<MyHomePage> {

  void initState() {
    super.initState();
    WidgetsBinding.instance
      .addTimingsCallback((timing) {
        print(1000 ~/ timing.first.totalSpan.inMilliseconds);
        setState(() {
          // here I change state
        });
      });
  }

  [...]

For some reason, the print statement shows that total timing is ~40ms, but the state is changing only few times a second. Why is that? What am I missing?

I launch the app using XCode in release mode.

K. Drab
  • 193
  • 1
  • 11
  • It looks like you're calling `setState` inside on `initState`. This is possible but you're probably getting the undesired behavior because of this. Are you able to make this `setState` call from the `build` method? – Apealed Nov 02 '20 at 20:52
  • Check [this discussion](https://stackoverflow.com/a/53373017/9727402) for a bit more on the behavior of calling `setState` inside `initState`. – Apealed Nov 02 '20 at 20:55
  • He’s not calling setstate from initstate – Pieter van Loon Nov 02 '20 at 21:05
  • Is the problem that the state is not changing as fast as you would expect or the 40ms. Also why do you divide 1000 by the amount of milliseconds? – Pieter van Loon Nov 02 '20 at 21:12
  • I call setState inside of the callback, not the `initState`. I divide to obtain FPS - (1000ms / 40ms) gives 25fps. The problem is that the setState is called like 5 times, but according to the times, it should be called 25. – K. Drab Nov 02 '20 at 21:48

0 Answers0