Files
swiftcontrol/test/widgets/go_pro_dialog_test.dart
Jonas Bark 5dbd091f27 Fix crash in Go Pro dialog when opener is unmounted
The dialog builder captured the caller's BuildContext for l10n lookups
and the purchase flow. When the widget that opened the dialog was
removed from the tree while the dialog was still showing (e.g. a
ConnectionCard remount), any rebuild of the dialog hit
AppLocalizations.of on the defunct context and crashed the isolate
with "Null check operator used on a null value" (l10n.dart:50 via
go_pro_dialog.dart:43, seen on Windows 6.0.0).

Use the dialog's own builder context instead, and guard the
post-purchase pop with a mounted check.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 21:41:23 +02:00

58 lines
1.9 KiB
Dart

import 'package:bike_control/gen/l10n.dart';
import 'package:bike_control/widgets/go_pro_dialog.dart';
import 'package:bike_control/widgets/ui/loading_widget.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shadcn_flutter/shadcn_flutter.dart';
Future<void> main() async {
await AppLocalizations.load(const Locale('en'));
testWidgets('dialog survives rebuild after its opener is removed from the tree', (tester) async {
final showOpener = ValueNotifier(true);
await tester.pumpWidget(
ShadcnApp(
localizationsDelegates: const [AppLocalizations.delegate],
supportedLocales: AppLocalizations.delegate.supportedLocales,
home: Scaffold(
child: ValueListenableBuilder(
valueListenable: showOpener,
builder: (context, visible, _) => visible ? const _Opener() : const SizedBox(),
),
),
),
);
await tester.tap(find.text('open'));
await tester.pumpAndSettle();
expect(find.text('Go Pro'), findsOneWidget);
// The widget that opened the dialog gets unmounted while the dialog is
// still showing (e.g. ConnectionCard remounting on device reconnect).
showOpener.value = false;
await tester.pump();
expect(find.text('Go Pro'), findsOneWidget);
// Any rebuild of the dialog's LoadingWidget (in production: setState when
// the Go Pro button is tapped) re-runs renderChild, which must not touch
// the opener's now-defunct context.
tester.element(find.byType(LoadingWidget)).markNeedsBuild();
await tester.pump();
expect(tester.takeException(), isNull);
expect(find.text('Go Pro'), findsOneWidget);
});
}
class _Opener extends StatelessWidget {
const _Opener();
@override
Widget build(BuildContext context) {
return Button.primary(
onPressed: () => showGoProDialog(context),
child: const Text('open'),
);
}
}