Files
swiftcontrol/lib/widgets/apps/zwift_tile.dart
Jonas Bark c682be43bc fix(ui): guard async setState/context against disposed widgets
Symbolicating a Rouvy user's 6.1 crashes (Shorebird patch build_id
4b7026f5fbf44e52452ba9e557c2c5a0) surfaced two "Null check operator used
on a null value" crashes: async UI callbacks running after their widget
was disposed (the user rapidly re-toggled the Zwift BLE option as
advertising kept failing).

- ZwiftTile.onChange: setState() ran after `await stopAllBleConnections`
  on an unmounted state -> guard with `mounted`.
- ConnectionMethod: openPermissionSheet(context, ...) and setState() ran
  after `await Future.wait(requirements)` on a defunct context -> guard
  both async gaps with `context.mounted` (matching the existing guard at
  line 315).

The underlying trigger (Zwift emulator "Data too large" advertise failure
on Rouvy) is tracked separately for the 6.2 branch.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-25 08:16:08 +02:00

82 lines
3.6 KiB
Dart

import 'package:bike_control/bluetooth/messages/notification.dart';
import 'package:bike_control/main.dart';
import 'package:bike_control/utils/core.dart';
import 'package:bike_control/utils/i18n_extension.dart';
import 'package:bike_control/utils/keymap/apps/rouvy.dart';
import 'package:bike_control/utils/keymap/apps/supported_app.dart';
import 'package:bike_control/widgets/ui/connection_method.dart';
import 'package:flutter/material.dart';
import 'package:prop/prop.dart';
class ZwiftTile extends StatefulWidget {
final bool small;
final VoidCallback onUpdate;
const ZwiftTile({super.key, required this.onUpdate, required this.small});
@override
State<ZwiftTile> createState() => _ZwiftTileState();
}
class _ZwiftTileState extends State<ZwiftTile> {
@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: core.zwiftEmulator.isConnected,
builder: (context, isConnected, _) {
return ValueListenableBuilder(
valueListenable: core.zwiftEmulator.isStarted,
builder: (context, isStarted, _) {
return StatefulBuilder(
builder: (context, setState) {
final isRouvy = core.settings.getTrainerApp() is Rouvy;
return ConnectionMethod(
small: widget.small,
trainerConnection: core.zwiftEmulator,
isRecommended: true,
supportLevel: core.settings.getTrainerApp()?.supportLevel(AppConnectionMethod.zwiftBle),
isEnabled: core.settings.getZwiftBleEmulatorEnabled(),
instructionLink: 'INSTRUCTIONS_ZWIFT.md',
onChange: (value) async {
if (value) {
await core.stopAllBleConnections();
}
core.settings.setZwiftBleEmulatorEnabled(value);
if (!value) {
core.zwiftEmulator.stopAdvertising();
} else if (value) {
core.zwiftEmulator.startAdvertising(widget.onUpdate).catchError((e, s) {
recordError(e, s, context: 'Zwift BLE Emulator');
core.zwiftEmulator.cleanup();
core.zwiftEmulator.isStarted.value = false;
core.settings.setZwiftBleEmulatorEnabled(false);
core.connection.signalNotification(AlertNotification(LogLevel.LOGLEVEL_ERROR, e.toString()));
});
}
// onChange awaits stopAllBleConnections above; the tile can
// be disposed mid-await (rapid toggling while advertising
// keeps failing), so guard setState — calling it unmounted
// throws "Null check operator used on a null value".
if (mounted) setState(() {});
},
title: context.i18n.connectUsingBluetooth,
description: !isStarted
? context.i18n.zwiftControllerDescription
: isConnected
? context.i18n.connected
: isRouvy
? context.i18n
.waitingForConnectionKickrBike(core.settings.getTrainerApp()?.name ?? '')
.replaceAll('KICKR BIKE PRO', 'BikeControl')
: context.i18n.waitingForConnectionKickrBike(core.settings.getTrainerApp()?.name ?? ''),
requirements: core.permissions.getRemoteControlRequirements(),
);
},
);
},
);
},
);
}
}