diff --git a/lib/screens/add_exercise_screen.dart b/lib/screens/add_exercise_screen.dart index e7fba2ed8..4370030f1 100644 --- a/lib/screens/add_exercise_screen.dart +++ b/lib/screens/add_exercise_screen.dart @@ -233,6 +233,9 @@ class EmailNotVerified extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final user = ref.watch(userProfileProvider).value; + if (user == null) { + return const Scaffold(body: Center(child: CircularProgressIndicator())); + } return Scaffold( appBar: EmptyAppBar(AppLocalizations.of(context).unVerifiedEmail), @@ -263,7 +266,7 @@ class EmailNotVerified extends ConsumerWidget { FormScreen.routeName, arguments: FormScreenArguments( AppLocalizations.of(context).userProfile, - UserProfileForm(user!), + UserProfileForm(user), ), ); }, diff --git a/lib/widgets/core/app_bar.dart b/lib/widgets/core/app_bar.dart index 1e32b0102..67e64a9ea 100644 --- a/lib/widgets/core/app_bar.dart +++ b/lib/widgets/core/app_bar.dart @@ -126,16 +126,22 @@ class MainSettingsDialog extends ConsumerWidget { //dense: true, leading: const Icon(Icons.person), title: Text(AppLocalizations.of(context).userProfile), - onTap: () => Navigator.pushNamed( - context, - FormScreen.routeName, - arguments: FormScreenArguments( - AppLocalizations.of(context).userProfile, - UserProfileForm( - ref.read(userProfileProvider).value!, + onTap: () { + // Only navigate once the profile is loaded; otherwise the form + // would crash on a null user. + final profile = ref.read(userProfileProvider).value; + if (profile == null) { + return; + } + Navigator.pushNamed( + context, + FormScreen.routeName, + arguments: FormScreenArguments( + AppLocalizations.of(context).userProfile, + UserProfileForm(profile), ), - ), - ), + ); + }, ), ListTile( leading: const Icon(Icons.settings), diff --git a/lib/widgets/dashboard/widgets/weight.dart b/lib/widgets/dashboard/widgets/weight.dart index e030ac91e..6fd2b2ab7 100644 --- a/lib/widgets/dashboard/widgets/weight.dart +++ b/lib/widgets/dashboard/widgets/weight.dart @@ -35,8 +35,8 @@ class DashboardWeightWidget extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { - final profile = ref.read(userProfileProvider).value; - final entriesList = ref.watch(weightEntryProvider); + final profileAsync = ref.watch(userProfileProvider); + final entriesAsync = ref.watch(weightEntryProvider); return Card( child: Column( @@ -52,8 +52,13 @@ class DashboardWeightWidget extends ConsumerWidget { color: Theme.of(context).textTheme.headlineSmall!.color, ), ), - entriesList.when( + entriesAsync.when( data: (entriesList) { + final profile = profileAsync.value; + if (profile == null) { + return const BoxedProgressIndicator(); + } + final (entriesAll, entries7dAvg) = sensibleRange( entriesList.map((e) => MeasurementChartEntry(e.weight, e.date)).toList(), ); @@ -67,7 +72,7 @@ class DashboardWeightWidget extends ConsumerWidget { height: 200, child: MeasurementChartWidgetFl( entriesAll, - weightUnit(profile!.isMetric, context), + weightUnit(profile.isMetric, context), avgs: entries7dAvg, ), ), diff --git a/lib/widgets/nutrition/nutritional_plans_list.dart b/lib/widgets/nutrition/nutritional_plans_list.dart index d66273f2e..b12e20120 100644 --- a/lib/widgets/nutrition/nutritional_plans_list.dart +++ b/lib/widgets/nutrition/nutritional_plans_list.dart @@ -54,9 +54,14 @@ class NutritionalPlansList extends riverpod.ConsumerWidget { // Format the weight change text and determine color final String weightChangeText; final Color weightChangeColor; - final profile = ref.read(userProfileProvider).value; + final profile = ref.watch(userProfileProvider).value; + if (profile == null) { + // Profile not yet loaded — skip the weight-change row entirely. The + // widget will rebuild with the value once available. + return const SizedBox.shrink(); + } - final unit = weightUnit(profile!.isMetric, context); + final unit = weightUnit(profile.isMetric, context); if (weightDifference > 0) { weightChangeText = '+${weightDifference.toStringAsFixed(1)} $unit'; diff --git a/lib/widgets/weight/weight_overview.dart b/lib/widgets/weight/weight_overview.dart index 1993b46ff..aef4acccb 100644 --- a/lib/widgets/weight/weight_overview.dart +++ b/lib/widgets/weight/weight_overview.dart @@ -35,17 +35,24 @@ class WeightOverview extends riverpod.ConsumerWidget { @override Widget build(BuildContext context, riverpod.WidgetRef ref) { - final profile = ref.read(userProfileProvider).value; + final profileAsync = ref.watch(userProfileProvider); final numberFormat = NumberFormat.decimalPattern(Localizations.localeOf(context).toString()); final plans = ref.watch(nutritionProvider).value ?? const []; final entries = ref.watch(weightEntryProvider); return entries.when( data: (entriesList) { + // Profile drives the unit display; show a spinner while it loads + // instead of bang-ing on a null value. + final profile = profileAsync.value; + if (profile == null) { + return const BoxedProgressIndicator(); + } + final entriesAll = entriesList.map((e) => MeasurementChartEntry(e.weight, e.date)).toList(); final entries7dAvg = moving7dAverage(entriesAll); - final unit = weightUnit(profile!.isMetric, context); + final unit = weightUnit(profile.isMetric, context); return Column( children: [