Member-only story

FLUTTER INTERVIEW QUESTIONS

What is a State object that you need to create in every StatefulWidget?

Jelena Lecic
2 min readJan 11, 2020

--

As we already know, Widgets are immutable(their fields must be final), and in order to be able to change widget’s appearance over time, we need to introduce the State class.

Everything starts with overriding createState() method in StatefulWidget:

… but, when createState() actually gets executed?

For this simple Flutter app:

import 'package:flutter/material.dart';

void main() => runApp(Muffin());

class Muffin extends StatefulWidget {
@override
_MuffinState createState() {
return _MuffinState();
}
}

class _MuffinState extends State<Muffin> {
@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return Container(
color: Colors.purple,
);
}
}

… this is order of methods that get executed before build() function is called:

So, State gets created before inserting its Element into the Element tree.

--

--

No responses yet