Skip to content

Commit 0fa9ecc

Browse files
open_wearable/lib/main.dart: fixed context used across async gaps
1 parent 5b0bead commit 0fa9ecc

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

open_wearable/lib/main.dart

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,34 +54,37 @@ class _MyAppState extends State<MyApp> {
5454
StreamSubscription? _unsupportedFirmwareSub;
5555

5656
@override
57-
void didChangeDependencies() {
58-
super.didChangeDependencies();
59-
// Avoid re-subscribing on hot reload
60-
_unsupportedFirmwareSub ??=
61-
context.read<WearablesProvider>().unsupportedFirmwareStream.listen((evt) async {
62-
final ctx = rootNavigatorKey.currentContext;
63-
if (ctx == null) return;
64-
if (!mounted) return;
57+
void initState() {
58+
super.initState();
6559

66-
// Guard context usage with mounted check after async gap
67-
await showPlatformDialog(
68-
context: ctx,
69-
builder: (_) => PlatformAlertDialog(
70-
title: const Text('Firmware nicht unterstützt'),
71-
content: Text(
72-
'The device "${evt.wearable.name}" has a firmware unsupported by this app. '
73-
'Please update the app to ensure all features are working as expected.',
74-
),
75-
actions: <Widget>[
76-
PlatformDialogAction(
77-
cupertino: (_, __) => CupertinoDialogActionData(isDefaultAction: true),
78-
child: const Text('OK'),
79-
onPressed: () {
80-
if (!mounted) return;
81-
Navigator.of(ctx, rootNavigator: true).pop();
82-
},
60+
// Read provider without listening, allowed in initState with Provider
61+
final wearables = context.read<WearablesProvider>();
62+
63+
_unsupportedFirmwareSub = wearables.unsupportedFirmwareStream.listen((evt) {
64+
// No async/await here. No widget context usage either.
65+
final nav = rootNavigatorKey.currentState;
66+
if (nav == null || !mounted) return;
67+
68+
// Push a dialog route via NavigatorState (no BuildContext from this widget)
69+
nav.push(
70+
DialogRoute<void>(
71+
context: rootNavigatorKey.currentContext!, // from navigator, not this widget
72+
barrierDismissible: true,
73+
builder: (_) => PlatformAlertDialog(
74+
title: const Text('Firmware unsupported'),
75+
content: Text(
76+
'The device "${evt.wearable.name}" has a firmware unsupported by this app. '
77+
'Please update the app to ensure all features are working as expected.',
8378
),
84-
],
79+
actions: <Widget>[
80+
PlatformDialogAction(
81+
cupertino: (_, __) => CupertinoDialogActionData(isDefaultAction: true),
82+
child: const Text('OK'),
83+
// Close via navigator state; no widget context
84+
onPressed: () => rootNavigatorKey.currentState?.pop(),
85+
),
86+
],
87+
),
8588
),
8689
);
8790
});

0 commit comments

Comments
 (0)