Flutter

Basic State Management

setState, InheritedWidget, Provider basics, scoped state, lifting state up

22 interview questionsยท
Mid-Level
1

What is the main purpose of setState in a StatefulWidget?

Answer

setState notifies the Flutter framework that the widget's internal state has changed and it needs to rebuild the widget. When setState is called, Flutter schedules a rebuild of the widget to reflect the new state values. Without calling setState, changes to state variables would not trigger a visual update.

2

What happens if a state variable is modified without calling setState?

Answer

Without calling setState, Flutter is not notified of the change and does not schedule a rebuild. The variable is modified in memory but the user interface remains unchanged until the next rebuild triggered by another cause. This is a common beginner mistake that leads to hard-to-identify bugs.

3

In what order should modifications be made when calling setState?

Answer

State modifications should be made inside the setState callback. This allows Flutter to trace changes and schedule the rebuild synchronously. While modifying before or after technically works, the convention is to modify inside for code clarity and change traceability.

4

Why is it discouraged to call setState in initState?

5

What is the main role of an InheritedWidget in Flutter?

+19 interview questions

Master Flutter for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free