fix: context.mounted check (and a bunch of auto format)
What changed, and why it matters
This commit fixes a Flutter widget state check in a cryptocurrency selection screen. It changes `mounted` to `context.mounted` before navigating back. The rest of the change is automatic code formatting. The fix prevents a potential app crash when a user presses the back button after the screen has already been disposed.
No immediate security action required. Treat as routine code-quality/hardening fix. Review whether similar `mounted` checks elsewhere in the codebase should be updated to `context.mounted` for consistency.
Security signals we found
Use-after-dispose guard added for async navigation
No security-relevant keywords in commit title or message
No incident, CVE, or advisory references present
Change is defensive/hardening rather than vulnerability remediation
Evidence from the diff
In a Flutter StatefulWidget (CryptoSelectionView), the back button handler previously checked mounted (the State object’s mounted property) before calling Navigator.of(context).pop(). The commit changes this to context.mounted, which checks whether the BuildContext is still associated with a mounted widget. The change is more semantically correct for async gaps and aligns with Flutter best practices. The remaining diff is purely dart format style changes with no functional effect.
Changed components
lib/pages/buy_view/sub_widgets/crypto_selection_view.dartInspect captured patch +44 / −45
diff --git a/lib/pages/buy_view/sub_widgets/crypto_selection_view.dart b/lib/pages/buy_view/sub_widgets/crypto_selection_view.dart
index 089b492..7f7705b 100644
--- a/lib/pages/buy_view/sub_widgets/crypto_selection_view.dart
+++ b/lib/pages/buy_view/sub_widgets/crypto_selection_view.dart
@@ -98,8 +98,9 @@ class _CryptoSelectionViewState extends ConsumerState<CryptoSelectionView> {
builder: (child) {
return Background(
child: Scaffold(
- backgroundColor:
- Theme.of(context).extension<StackColors>()!.background,
+ backgroundColor: Theme.of(
+ context,
+ ).extension<StackColors>()!.background,
appBar: AppBar(
leading: AppBarBackButton(
onPressed: () async {
@@ -109,7 +110,7 @@ class _CryptoSelectionViewState extends ConsumerState<CryptoSelectionView> {
const Duration(milliseconds: 50),
);
}
- if (mounted) {
+ if (context.mounted) {
Navigator.of(context).pop();
}
},
@@ -145,45 +146,45 @@ class _CryptoSelectionViewState extends ConsumerState<CryptoSelectionView> {
focusNode: _searchFocusNode,
onChanged: filter,
style: STextStyles.field(context),
- decoration: standardInputDecoration(
- "Search",
- _searchFocusNode,
- context,
- desktopMed: isDesktop,
- ).copyWith(
- prefixIcon: Padding(
- padding: const EdgeInsets.symmetric(
- horizontal: 10,
- vertical: 16,
- ),
- child: SvgPicture.asset(
- Assets.svg.search,
- width: 16,
- height: 16,
- ),
- ),
- suffixIcon:
- _searchController.text.isNotEmpty
+ decoration:
+ standardInputDecoration(
+ "Search",
+ _searchFocusNode,
+ context,
+ desktopMed: isDesktop,
+ ).copyWith(
+ prefixIcon: Padding(
+ padding: const EdgeInsets.symmetric(
+ horizontal: 10,
+ vertical: 16,
+ ),
+ child: SvgPicture.asset(
+ Assets.svg.search,
+ width: 16,
+ height: 16,
+ ),
+ ),
+ suffixIcon: _searchController.text.isNotEmpty
? Padding(
- padding: const EdgeInsets.only(right: 0),
- child: UnconstrainedBox(
- child: Row(
- children: [
- TextFieldIconButton(
- child: const XIcon(),
- onTap: () async {
- setState(() {
- _searchController.text = "";
- });
- filter("");
- },
- ),
- ],
+ padding: const EdgeInsets.only(right: 0),
+ child: UnconstrainedBox(
+ child: Row(
+ children: [
+ TextFieldIconButton(
+ child: const XIcon(),
+ onTap: () async {
+ setState(() {
+ _searchController.text = "";
+ });
+ filter("");
+ },
+ ),
+ ],
+ ),
),
- ),
- )
+ )
: null,
- ),
+ ),
),
),
const SizedBox(height: 10),
@@ -226,14 +227,12 @@ class _CryptoSelectionViewState extends ConsumerState<CryptoSelectionView> {
const SizedBox(height: 2),
Text(
_coins[index].ticker.toUpperCase(),
- style: STextStyles.smallMed12(
- context,
- ).copyWith(
- color:
- Theme.of(context)
+ style: STextStyles.smallMed12(context)
+ .copyWith(
+ color: Theme.of(context)
.extension<StackColors>()!
.textSubtitle1,
- ),
+ ),
),
],
),
Why this scored 16/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.