What is the mounted property and why is it important in async code?
TL;DR: mounted is a boolean on State that is true when the State is in the widget tree and false after dispose() is called. Always check mounted before calling setState() or using BuildContext after an async gap.
Full Answer
In Dart, async operations can complete after a widget has been removed from the tree. If you call setState() on a disposed widget, Flutter throws an error. The mounted guard prevents this.
Never call setState(), context.read(), Navigator.of(context), or any context operation after an async gap without checking mounted. This is one of the most common Flutter bugs.
Code Examples
If widget is still mounted: updates results list. If widget was disposed during fetch: silently returns without error.
Interview Tip
In Flutter 3.7+, using context after an async gap in a StatefulWidget triggers a lint warning. For StatelessWidgets, this isn't an issue since they have no lifecycle. Consider using Riverpod ref.read() which handles this pattern automatically.