Search code examples
flutterdartstatefulwidget

Second screen doesn't appear until initState is done


I have StatefulWidget which initState() triggers async onInit() function that triggers a top level function that returns list of items, then the onInit populates List of items. Then I have Scaffold which body should return CircularProgressIndicator until items == null and then Column with the items.

The thing is that I tap button to navigate to the second screen it takes like 3-4 seconds until the screen appears and when it does, the list is populated. I'm pretty sure that the function doesn't let the screen to appear until it's done, but I have no idea why?

Why this behavior if it's supposed to navigate to the second screen then start initState and show loading indicator until the func is done?

Can anyone help me with this, please?


Solution

  • In this case because the function I trigger on initState() makes some heavy calculations and because Dart is single thread language and because CPU is separated from I/O it starts the calculations and until the function is done it doesn't build the widget.

    Because of this I opted to use an Isolate!