A surprising number of developers aren’t fully aware of the difference between imperative and declarative coding. Yet this idea is crucial to understand, especially because
#FlutterDev is a declarative framework.
Welcome to "Mastering the Flutter Fundamentals" series (3/n)
In an imperative style, you tell the system exactly how to do something: every step/mutation/instruction.
// Imperative example
button.setText("Submit");
button.setColor("blue");
button.moveTo(x: 20, y: 100);
In a declarative style, you describe what the UI should look like, given the current state. The framework figures out the underlying steps.
// Declarative example
class SubmitButton extends StatelessWidget {
final bool isLoading;
const SubmitButton({required this.isLoading});
@ override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: isLoading ? null : () {},
child: Text(isLoading ? "Loading..." : "Submit"),
);
}
}
No manual mutations.
Just a description: if the state is X, the UI looks like Y.
And hence, UI = f(state)
The result is code that behaves more predictably, reads more cleanly, and avoids a whole class of bugs caused by scattered changes.