Guard setState after await with mounted checks
What changed, and why it matters
This commit adds safety checks in a Flutter mobile wallet app to prevent the app from trying to update a screen that the user has already navigated away from. After an async operation like a network call or QR scan, the code now checks whether the widget is still 'mounted' before calling setState. Without these guards, the app could crash or throw exceptions in the background, but this is a stability fix rather than a security vulnerability.
No security response required. Treat as a routine stability/quality improvement. Continue normal testing and release processes.
Security signals we found
Defensive lifecycle guard added after async boundaries
No change to trust boundaries, authentication, cryptography, or network validation
No input sanitization, output encoding, or permission changes
Evidence from the diff
The patch inserts if (!mounted) return; guards after await points and before setState() calls across four Dart/Flutter files. In Flutter, calling setState on a disposed widget throws a ‘setState() called after dispose()’ error. The affected flows include wallet restoration, send/validation, OpenAlias resolution, settings loading, and connection/QR scanning. The change is defensive programming to avoid lifecycle races.
Changed components
lib/screens/restore_wallet.dartlib/screens/send.dartlib/screens/settings.dartlib/widgets/connection_settings_form.dartInspect captured patch +12 / −1
### lib/screens/restore_wallet.dart
@@ -96,6 +96,7 @@ class _RestoreWalletScreenState extends State<RestoreWalletScreen> with SecureSc
locale: Localizations.localeOf(context).toString(),
),
);
+ if (!mounted) return;
if (result == null) return;
setState(() {
### lib/screens/send.dart
@@ -312,7 +312,9 @@ class _SendScreenState extends State<SendScreen> {
if (domainRegex.hasMatch(unresolvedDestinationAddress)) {
// OpenAlias: cached + deduped; the counter gates the send button while
// a lookup is in flight.
- if (await _resolveDomain(unresolvedDestinationAddress) == null) {
+ final resolved = await _resolveDomain(unresolvedDestinationAddress);
+ if (!mounted) return false;
+ if (resolved == null) {
if (setErrors) {
setState(() {
_destinationAddressError = i18n.sendOpenAliasResolveError;
@@ -420,6 +422,7 @@ class _SendScreenState extends State<SendScreen> {
});
final isValid = await _validateForm();
+ if (!mounted) return;
if (!isValid) {
setState(() {
@@ -437,6 +440,7 @@ class _SendScreenState extends State<SendScreen> {
// it, so this comes back from the cache rather than hitting Tor again.
if (domainRegex.hasMatch(destinationAddressUnresolved)) {
final resolved = await _resolveDomain(destinationAddressUnresolved);
+ if (!mounted) return;
if (resolved == null) {
setState(() {
@@ -476,6 +480,7 @@ class _SendScreenState extends State<SendScreen> {
);
}
} catch (error) {
+ if (!mounted) return;
if (error.toString().contains('Unlocked funds too low')) {
// Display-only: picks which of two error messages to show, so the
// imprecision of a double cannot reach an amount anyone spends.
@@ -496,6 +501,7 @@ class _SendScreenState extends State<SendScreen> {
}
}
+ if (!mounted) return;
setState(() {
_isLoading = false;
});
### lib/screens/settings.dart
@@ -70,6 +70,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
void _loadPackageInfo() async {
final packageInfo = await PackageInfo.fromPlatform();
+ if (!mounted) return;
setState(() {
_appVersion = packageInfo.version;
_buildNumber = packageInfo.buildNumber;
@@ -140,6 +141,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
}
}
+ if (!mounted) return;
setState(() {
_appLockEnabled = value;
});
### lib/widgets/connection_settings_form.dart
@@ -163,6 +163,7 @@ class _ConnectionSettingsFormState extends State<ConnectionSettingsForm> {
Future<void> _loadPersistedConnection() async {
final wallet = appWalletOf(context);
final conn = await wallet.getPersistedConnection();
+ if (!mounted) return;
final useTor = conn.useTor && TorSettingsService.sharedInstance.torMode != TorMode.disabled;
final type = connectionTypeOptions.contains(conn.connectionType) ? conn.connectionType : 'lws';
@@ -189,6 +190,7 @@ class _ConnectionSettingsFormState extends State<ConnectionSettingsForm> {
final i18n = AppLocalizations.of(context)!;
final result = await Navigator.pushNamed(context, '/scan_qr');
+ if (!mounted) return;
if (result != null && result is String) {
final scannedAddress = cleanConnectionAddress(result);Why this scored 26/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.