Skip to content

Lifecycle/Rendering in Flutter

Flutter, Lifecycle1 min read

The Issue

I was testing out Flutter’s core Navigation(which is great by the way) and I wanted add an animated Splash Screen to my project. The idea was to load some data then show an animation on a logo on the screen and transition to the main screen of the app. Usually in React Native I can rely on componentDidMount to run this kind of logic. I went searching for something similar in Flutter and I couldn’t find it. I tried running the tasks in initState but this didn’t work either. Instead I was greeted with a pretty clear warning about not being able to modify state while a widget is building.

A Solution

After a little more research I came across a method from Dart’s async library called scheduleMicroTask. When I wrapped my transition logic in this inside initState the error disappeared and things worked great:

2void initState() {
3 super.initState();
4 scheduleMicrotask(this.transition);
7transition() {
8 // Logic