Properly check if the userprofile was correctly loaded

This commit is contained in:
Roland Geider
2026-04-18 16:15:10 +02:00
parent c1a733b423
commit 8252dcdd74
5 changed files with 44 additions and 18 deletions

View File

@@ -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),
),
);
},

View File

@@ -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),

View File

@@ -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,
),
),

View File

@@ -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';

View File

@@ -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: [