diff --git a/lib/database/powersync/powersync.g.dart b/lib/database/powersync/powersync.g.dart index 34e669787..08e2c6af3 100644 --- a/lib/database/powersync/powersync.g.dart +++ b/lib/database/powersync/powersync.g.dart @@ -46,7 +46,7 @@ final class PowerSyncInstanceProvider } } -String _$powerSyncInstanceHash() => r'cc51119f46b1a2838312e5bb60cec1db8748caac'; +String _$powerSyncInstanceHash() => r'25fdd0cce6fad18d2859035f1d5475caac992dfb'; @ProviderFor(didCompleteSync) final didCompleteSyncProvider = DidCompleteSyncFamily._(); diff --git a/lib/main.dart b/lib/main.dart index b88fd332f..d8391ca4e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -137,7 +137,7 @@ class MainApp extends ConsumerWidget { Widget _getHomeScreen(AuthState auth) { switch (auth.status) { case AuthStatus.loggedIn: - return const EagerInitialization(); + return const HomeTabsScreen(); case AuthStatus.updateRequired: return const UpdateAppScreen(); case AuthStatus.serverUpdateRequired: @@ -175,7 +175,7 @@ class MainApp extends ConsumerWidget { FormScreen.routeName: (ctx) => const FormScreen(), GalleryScreen.routeName: (ctx) => const GalleryScreen(), GymModeScreen.routeName: (ctx) => const GymModeScreen(), - HomeTabsScreen.routeName: (ctx) => HomeTabsScreen(), + HomeTabsScreen.routeName: (ctx) => const HomeTabsScreen(), MeasurementCategoriesScreen.routeName: (ctx) => const MeasurementCategoriesScreen(), MeasurementEntriesScreen.routeName: (ctx) => const MeasurementEntriesScreen(), NutritionalPlansScreen.routeName: (ctx) => const NutritionalPlansScreen(), diff --git a/lib/providers/auth_notifier.dart b/lib/providers/auth_notifier.dart index b50f1d4d5..fc8c20f5c 100644 --- a/lib/providers/auth_notifier.dart +++ b/lib/providers/auth_notifier.dart @@ -313,11 +313,6 @@ class AuthNotifier extends _$AuthNotifier { state = AsyncData(current.copyWith(serverVersion: v)); } - /// Marks that the initial eager data-load has completed. - void setDataInit(bool value) { - state = AsyncData(_currentOrBlank().copyWith(dataInit: value)); - } - Future _fetchServerVersion(String serverUrl) async { final response = await _client.get(makeUri(serverUrl, SERVER_VERSION_URL)); return json.decode(response.body); diff --git a/lib/providers/auth_notifier.g.dart b/lib/providers/auth_notifier.g.dart index 4aea70185..630ea38aa 100644 --- a/lib/providers/auth_notifier.g.dart +++ b/lib/providers/auth_notifier.g.dart @@ -32,7 +32,7 @@ final class AuthNotifierProvider extends $AsyncNotifierProvider AuthNotifier(); } -String _$authNotifierHash() => r'a0d999826d2a4b46be8fc40ca1466c0b1690797f'; +String _$authNotifierHash() => r'48b66ac1508c92dea7daa3a0c091e9f7f56aa28a'; abstract class _$AuthNotifier extends $AsyncNotifier { FutureOr build(); diff --git a/lib/providers/auth_state.dart b/lib/providers/auth_state.dart index 4237fcc8f..c6cf69a40 100644 --- a/lib/providers/auth_state.dart +++ b/lib/providers/auth_state.dart @@ -41,7 +41,6 @@ sealed class AuthState with _$AuthState { String? serverUrl, String? serverVersion, PackageInfo? applicationVersion, - @Default(false) bool dataInit, }) = _AuthState; const AuthState._(); diff --git a/lib/providers/auth_state.freezed.dart b/lib/providers/auth_state.freezed.dart index bf14d93bd..086a03b3f 100644 --- a/lib/providers/auth_state.freezed.dart +++ b/lib/providers/auth_state.freezed.dart @@ -14,7 +14,7 @@ T _$identity(T value) => value; /// @nodoc mixin _$AuthState { - AuthStatus get status; String? get token; String? get serverUrl; String? get serverVersion; PackageInfo? get applicationVersion; bool get dataInit; + AuthStatus get status; String? get token; String? get serverUrl; String? get serverVersion; PackageInfo? get applicationVersion; /// Create a copy of AuthState /// with the given fields replaced by the non-null parameter values. @JsonKey(includeFromJson: false, includeToJson: false) @@ -25,16 +25,16 @@ $AuthStateCopyWith get copyWith => _$AuthStateCopyWithImpl @override bool operator ==(Object other) { - return identical(this, other) || (other.runtimeType == runtimeType&&other is AuthState&&(identical(other.status, status) || other.status == status)&&(identical(other.token, token) || other.token == token)&&(identical(other.serverUrl, serverUrl) || other.serverUrl == serverUrl)&&(identical(other.serverVersion, serverVersion) || other.serverVersion == serverVersion)&&(identical(other.applicationVersion, applicationVersion) || other.applicationVersion == applicationVersion)&&(identical(other.dataInit, dataInit) || other.dataInit == dataInit)); + return identical(this, other) || (other.runtimeType == runtimeType&&other is AuthState&&(identical(other.status, status) || other.status == status)&&(identical(other.token, token) || other.token == token)&&(identical(other.serverUrl, serverUrl) || other.serverUrl == serverUrl)&&(identical(other.serverVersion, serverVersion) || other.serverVersion == serverVersion)&&(identical(other.applicationVersion, applicationVersion) || other.applicationVersion == applicationVersion)); } @override -int get hashCode => Object.hash(runtimeType,status,token,serverUrl,serverVersion,applicationVersion,dataInit); +int get hashCode => Object.hash(runtimeType,status,token,serverUrl,serverVersion,applicationVersion); @override String toString() { - return 'AuthState(status: $status, token: $token, serverUrl: $serverUrl, serverVersion: $serverVersion, applicationVersion: $applicationVersion, dataInit: $dataInit)'; + return 'AuthState(status: $status, token: $token, serverUrl: $serverUrl, serverVersion: $serverVersion, applicationVersion: $applicationVersion)'; } @@ -45,7 +45,7 @@ abstract mixin class $AuthStateCopyWith<$Res> { factory $AuthStateCopyWith(AuthState value, $Res Function(AuthState) _then) = _$AuthStateCopyWithImpl; @useResult $Res call({ - AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion, bool dataInit + AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion }); @@ -62,15 +62,14 @@ class _$AuthStateCopyWithImpl<$Res> /// Create a copy of AuthState /// with the given fields replaced by the non-null parameter values. -@pragma('vm:prefer-inline') @override $Res call({Object? status = null,Object? token = freezed,Object? serverUrl = freezed,Object? serverVersion = freezed,Object? applicationVersion = freezed,Object? dataInit = null,}) { +@pragma('vm:prefer-inline') @override $Res call({Object? status = null,Object? token = freezed,Object? serverUrl = freezed,Object? serverVersion = freezed,Object? applicationVersion = freezed,}) { return _then(_self.copyWith( status: null == status ? _self.status : status // ignore: cast_nullable_to_non_nullable as AuthStatus,token: freezed == token ? _self.token : token // ignore: cast_nullable_to_non_nullable as String?,serverUrl: freezed == serverUrl ? _self.serverUrl : serverUrl // ignore: cast_nullable_to_non_nullable as String?,serverVersion: freezed == serverVersion ? _self.serverVersion : serverVersion // ignore: cast_nullable_to_non_nullable as String?,applicationVersion: freezed == applicationVersion ? _self.applicationVersion : applicationVersion // ignore: cast_nullable_to_non_nullable -as PackageInfo?,dataInit: null == dataInit ? _self.dataInit : dataInit // ignore: cast_nullable_to_non_nullable -as bool, +as PackageInfo?, )); } @@ -152,10 +151,10 @@ return $default(_that);case _: /// } /// ``` -@optionalTypeArgs TResult maybeWhen(TResult Function( AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion, bool dataInit)? $default,{required TResult orElse(),}) {final _that = this; +@optionalTypeArgs TResult maybeWhen(TResult Function( AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion)? $default,{required TResult orElse(),}) {final _that = this; switch (_that) { case _AuthState() when $default != null: -return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_that.applicationVersion,_that.dataInit);case _: +return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_that.applicationVersion);case _: return orElse(); } @@ -173,10 +172,10 @@ return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_th /// } /// ``` -@optionalTypeArgs TResult when(TResult Function( AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion, bool dataInit) $default,) {final _that = this; +@optionalTypeArgs TResult when(TResult Function( AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion) $default,) {final _that = this; switch (_that) { case _AuthState(): -return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_that.applicationVersion,_that.dataInit);} +return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_that.applicationVersion);} } /// A variant of `when` that fallback to returning `null` /// @@ -190,10 +189,10 @@ return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_th /// } /// ``` -@optionalTypeArgs TResult? whenOrNull(TResult? Function( AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion, bool dataInit)? $default,) {final _that = this; +@optionalTypeArgs TResult? whenOrNull(TResult? Function( AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion)? $default,) {final _that = this; switch (_that) { case _AuthState() when $default != null: -return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_that.applicationVersion,_that.dataInit);case _: +return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_that.applicationVersion);case _: return null; } @@ -205,7 +204,7 @@ return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_th class _AuthState extends AuthState { - const _AuthState({this.status = AuthStatus.loggedOut, this.token, this.serverUrl, this.serverVersion, this.applicationVersion, this.dataInit = false}): super._(); + const _AuthState({this.status = AuthStatus.loggedOut, this.token, this.serverUrl, this.serverVersion, this.applicationVersion}): super._(); @override@JsonKey() final AuthStatus status; @@ -213,7 +212,6 @@ class _AuthState extends AuthState { @override final String? serverUrl; @override final String? serverVersion; @override final PackageInfo? applicationVersion; -@override@JsonKey() final bool dataInit; /// Create a copy of AuthState /// with the given fields replaced by the non-null parameter values. @@ -225,16 +223,16 @@ _$AuthStateCopyWith<_AuthState> get copyWith => __$AuthStateCopyWithImpl<_AuthSt @override bool operator ==(Object other) { - return identical(this, other) || (other.runtimeType == runtimeType&&other is _AuthState&&(identical(other.status, status) || other.status == status)&&(identical(other.token, token) || other.token == token)&&(identical(other.serverUrl, serverUrl) || other.serverUrl == serverUrl)&&(identical(other.serverVersion, serverVersion) || other.serverVersion == serverVersion)&&(identical(other.applicationVersion, applicationVersion) || other.applicationVersion == applicationVersion)&&(identical(other.dataInit, dataInit) || other.dataInit == dataInit)); + return identical(this, other) || (other.runtimeType == runtimeType&&other is _AuthState&&(identical(other.status, status) || other.status == status)&&(identical(other.token, token) || other.token == token)&&(identical(other.serverUrl, serverUrl) || other.serverUrl == serverUrl)&&(identical(other.serverVersion, serverVersion) || other.serverVersion == serverVersion)&&(identical(other.applicationVersion, applicationVersion) || other.applicationVersion == applicationVersion)); } @override -int get hashCode => Object.hash(runtimeType,status,token,serverUrl,serverVersion,applicationVersion,dataInit); +int get hashCode => Object.hash(runtimeType,status,token,serverUrl,serverVersion,applicationVersion); @override String toString() { - return 'AuthState(status: $status, token: $token, serverUrl: $serverUrl, serverVersion: $serverVersion, applicationVersion: $applicationVersion, dataInit: $dataInit)'; + return 'AuthState(status: $status, token: $token, serverUrl: $serverUrl, serverVersion: $serverVersion, applicationVersion: $applicationVersion)'; } @@ -245,7 +243,7 @@ abstract mixin class _$AuthStateCopyWith<$Res> implements $AuthStateCopyWith<$Re factory _$AuthStateCopyWith(_AuthState value, $Res Function(_AuthState) _then) = __$AuthStateCopyWithImpl; @override @useResult $Res call({ - AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion, bool dataInit + AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion }); @@ -262,15 +260,14 @@ class __$AuthStateCopyWithImpl<$Res> /// Create a copy of AuthState /// with the given fields replaced by the non-null parameter values. -@override @pragma('vm:prefer-inline') $Res call({Object? status = null,Object? token = freezed,Object? serverUrl = freezed,Object? serverVersion = freezed,Object? applicationVersion = freezed,Object? dataInit = null,}) { +@override @pragma('vm:prefer-inline') $Res call({Object? status = null,Object? token = freezed,Object? serverUrl = freezed,Object? serverVersion = freezed,Object? applicationVersion = freezed,}) { return _then(_AuthState( status: null == status ? _self.status : status // ignore: cast_nullable_to_non_nullable as AuthStatus,token: freezed == token ? _self.token : token // ignore: cast_nullable_to_non_nullable as String?,serverUrl: freezed == serverUrl ? _self.serverUrl : serverUrl // ignore: cast_nullable_to_non_nullable as String?,serverVersion: freezed == serverVersion ? _self.serverVersion : serverVersion // ignore: cast_nullable_to_non_nullable as String?,applicationVersion: freezed == applicationVersion ? _self.applicationVersion : applicationVersion // ignore: cast_nullable_to_non_nullable -as PackageInfo?,dataInit: null == dataInit ? _self.dataInit : dataInit // ignore: cast_nullable_to_non_nullable -as bool, +as PackageInfo?, )); } diff --git a/lib/providers/nutrition_notifier.dart b/lib/providers/nutrition_notifier.dart index 832a63f8f..942e05a84 100644 --- a/lib/providers/nutrition_notifier.dart +++ b/lib/providers/nutrition_notifier.dart @@ -44,7 +44,13 @@ class NutritionNotifier extends _$NutritionNotifier { Future> build() async { _nutritionRepo = ref.read(nutritionRepositoryProvider); _ingredientRepo = ref.read(ingredientRepositoryProvider); - return []; + + // Auto-fetch the sparse plan list on first read. Consumers that need + // a forced refresh (e.g. RefreshIndicator) can still call + // [fetchAndSetAllPlansSparse] explicitly. + final data = await _nutritionRepo.fetchAllPlans(); + return data.map((e) => NutritionalPlan.fromJson(e)).toList() + ..sort((a, b) => b.creationDate.compareTo(a.creationDate)); } List get _plans => state.asData?.value ?? []; diff --git a/lib/providers/nutrition_notifier.g.dart b/lib/providers/nutrition_notifier.g.dart index 8322b1fe5..162ea2d03 100644 --- a/lib/providers/nutrition_notifier.g.dart +++ b/lib/providers/nutrition_notifier.g.dart @@ -33,7 +33,7 @@ final class NutritionNotifierProvider NutritionNotifier create() => NutritionNotifier(); } -String _$nutritionNotifierHash() => r'a96cda5646bff0a63b53bf70793370915e92bd9c'; +String _$nutritionNotifierHash() => r'958bf02bc0aa8dc739be26a04b4efbb1004bfd72'; abstract class _$NutritionNotifier extends $AsyncNotifier> { FutureOr> build(); diff --git a/lib/providers/routines.dart b/lib/providers/routines.dart index b0e39f870..5e2ab7cfc 100644 --- a/lib/providers/routines.dart +++ b/lib/providers/routines.dart @@ -96,6 +96,18 @@ class RoutinesRiverpod extends _$RoutinesRiverpod { @override RoutinesState build() { _logger.fine('Building Routines Riverpod notifier'); + + // Kick off the initial sparse load. UI consumers `watch`ing this + // provider rebuild automatically once `state` is updated. Errors are + // logged but don't crash the build. + Future.microtask(() async { + try { + await fetchAllRoutinesSparse(); + } catch (e, s) { + _logger.warning('initial routine fetch failed', e, s); + } + }); + return const RoutinesState(); } diff --git a/lib/providers/routines.g.dart b/lib/providers/routines.g.dart index bb510ce7d..c0b7c1a98 100644 --- a/lib/providers/routines.g.dart +++ b/lib/providers/routines.g.dart @@ -118,7 +118,7 @@ final class RoutinesRiverpodProvider extends $NotifierProvider r'7a097555c9718249a21328ac16234ef26e23c453'; +String _$routinesRiverpodHash() => r'83e7e418dcd7adc36e8897598927c6a7b47a33c9'; abstract class _$RoutinesRiverpod extends $Notifier { RoutinesState build(); diff --git a/lib/providers/trophies.dart b/lib/providers/trophies.dart index 19289af58..5c384b514 100644 --- a/lib/providers/trophies.dart +++ b/lib/providers/trophies.dart @@ -132,6 +132,16 @@ final class TrophyStateNotifier extends _$TrophyStateNotifier { @override TrophyState build() { + // Kick off the initial trophy load. Sync notifier, so we can't await + // here — UI consumers rebuild via state mutation when data arrives. + Future.microtask(() async { + try { + await fetchAll(); + } catch (e, s) { + _logger.warning('initial trophy fetch failed', e, s); + } + }); + return TrophyState(); } diff --git a/lib/providers/trophies.g.dart b/lib/providers/trophies.g.dart index 3f1ffecd2..ebe7551c1 100644 --- a/lib/providers/trophies.g.dart +++ b/lib/providers/trophies.g.dart @@ -82,7 +82,7 @@ final class TrophyStateNotifierProvider } } -String _$trophyStateNotifierHash() => r'c80c732272cf843b698f28152f60b9a5f37ee449'; +String _$trophyStateNotifierHash() => r'624d9b9f4e6fb2c8a31a1e7c5dd9ec8dce870c2f'; abstract class _$TrophyStateNotifier extends $Notifier { TrophyState build(); diff --git a/lib/widgets/dashboard/calendar.dart b/lib/widgets/dashboard/calendar.dart index 63c08b36a..dd907f079 100644 --- a/lib/widgets/dashboard/calendar.dart +++ b/lib/widgets/dashboard/calendar.dart @@ -80,16 +80,6 @@ class _DashboardCalendarWidgetState extends riverpod.ConsumerState>>(weightEntryProvider, (prev, next) { - loadEvents(); - }); - - ref.listen>>(measurementProvider, (prev, next) { - loadEvents(); - }); } /// Loads and organizes all events from various providers into the calendar. @@ -257,6 +247,21 @@ class _DashboardCalendarWidgetState extends riverpod.ConsumerState>>(weightEntryProvider, (prev, next) { + loadEvents(); + }); + ref.listen>>(measurementProvider, (prev, next) { + loadEvents(); + }); + ref.listen(nutritionProvider, (prev, next) { + loadEvents(); + }); + return Card( child: Column( children: [ diff --git a/lib/widgets/dashboard/widgets/nutrition.dart b/lib/widgets/dashboard/widgets/nutrition.dart index 3b0df8603..efc72c981 100644 --- a/lib/widgets/dashboard/widgets/nutrition.dart +++ b/lib/widgets/dashboard/widgets/nutrition.dart @@ -1,13 +1,13 @@ /* * This file is part of wger Workout Manager . - * Copyright (C) 2020, 2021 wger Team + * Copyright (c) 2020 - 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * - * wger Workout Manager is distributed in the hope that it will be useful, + * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. @@ -21,7 +21,6 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg_icons/flutter_svg_icons.dart'; import 'package:intl/intl.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; -import 'package:wger/models/nutrition/nutritional_plan.dart'; import 'package:wger/providers/nutrition_notifier.dart'; import 'package:wger/screens/form_screen.dart'; import 'package:wger/screens/log_meals_screen.dart'; @@ -30,39 +29,30 @@ import 'package:wger/widgets/dashboard/widgets/nothing_found.dart'; import 'package:wger/widgets/nutrition/charts.dart'; import 'package:wger/widgets/nutrition/forms.dart'; -class DashboardNutritionWidget extends ConsumerStatefulWidget { +class DashboardNutritionWidget extends ConsumerWidget { const DashboardNutritionWidget(); @override - ConsumerState createState() => _DashboardNutritionWidgetState(); -} + Widget build(BuildContext context, WidgetRef ref) { + // Watch so the widget rebuilds when plans arrive from the initial fetch. + // currentPlan is derived from the watched state, so no separate read. + ref.watch(nutritionProvider); + final plan = ref.read(nutritionProvider.notifier).currentPlan; + final hasContent = plan != null; -class _DashboardNutritionWidgetState extends ConsumerState { - NutritionalPlan? _plan; - bool _hasContent = false; - - @override - void initState() { - super.initState(); - _plan = ref.read(nutritionProvider.notifier).currentPlan; - _hasContent = _plan != null; - } - - @override - Widget build(BuildContext context) { return Card( child: Column( children: [ ListTile( title: Text( - _hasContent ? _plan!.description : AppLocalizations.of(context).nutritionalPlan, + hasContent ? plan.description : AppLocalizations.of(context).nutritionalPlan, style: Theme.of(context).textTheme.headlineSmall, ), subtitle: Text( - _hasContent + hasContent ? DateFormat.yMd( Localizations.localeOf(context).languageCode, - ).format(_plan!.creationDate) + ).format(plan.creationDate) : '', ), leading: Icon( @@ -70,12 +60,12 @@ class _DashboardNutritionWidgetState extends ConsumerState null); + + // NutritionNotifier.build() auto-fetches all plans on first read. + // Default to an empty list so tests that don't care about the plan list + // don't have to stub it; tests that need real plans override via + // [containerWithPlans]. + when(mockRepo.fetchAllPlans()).thenAnswer((_) async => []); }); group('fetchAndSetPlanFull', () { diff --git a/test/nutrition/nutritional_meal_form_test.dart b/test/nutrition/nutritional_meal_form_test.dart index eb9526e1a..3272ce90d 100644 --- a/test/nutrition/nutritional_meal_form_test.dart +++ b/test/nutrition/nutritional_meal_form_test.dart @@ -51,6 +51,9 @@ void main() { when(mockRepo.updateMeal(any, any)).thenAnswer((_) async => Meal(id: 1, plan: 1).toJson()); when(mockRepo.createMeal(any)).thenAnswer((_) async => Meal(id: 99, plan: 1).toJson()); + // NutritionNotifier.build() auto-fetches plans on first read; the + // explicit state seed below replaces this empty value. + when(mockRepo.fetchAllPlans()).thenAnswer((_) async => []); container = ProviderContainer( overrides: [ diff --git a/test/nutrition/nutritional_meal_item_form_test.dart b/test/nutrition/nutritional_meal_item_form_test.dart index c57b60465..85f37e885 100644 --- a/test/nutrition/nutritional_meal_item_form_test.dart +++ b/test/nutrition/nutritional_meal_item_form_test.dart @@ -126,6 +126,8 @@ void main() { when(mockRepo.fetchIngredient(any)).thenAnswer((_) async => ingredient); when(mockRepo.fetchWeightUnits(1)).thenAnswer((_) => Future.value([])); when(mockRepo.fetchWeightUnits(2)).thenAnswer((_) => Future.value([])); + // NutritionNotifier.build() auto-fetches plans on first read. + when(mockRepo.fetchAllPlans()).thenAnswer((_) async => []); container = ProviderContainer( overrides: [ diff --git a/test/nutrition/nutritional_plan_form_test.dart b/test/nutrition/nutritional_plan_form_test.dart index c3a1601db..2010d4004 100644 --- a/test/nutrition/nutritional_plan_form_test.dart +++ b/test/nutrition/nutritional_plan_form_test.dart @@ -51,6 +51,8 @@ void main() { when(mockRepo.updatePlan(any, any)).thenAnswer((_) async {}); when(mockRepo.createPlan(any)).thenAnswer((_) async => plan1.toJson()); + // NutritionNotifier.build() auto-fetches plans on first read. + when(mockRepo.fetchAllPlans()).thenAnswer((_) async => []); }); Widget createHomeScreen(NutritionalPlan plan, {locale = 'en'}) { diff --git a/test/nutrition/nutritional_plan_screen_test.dart b/test/nutrition/nutritional_plan_screen_test.dart index 3e67286e2..2507d116f 100644 --- a/test/nutrition/nutritional_plan_screen_test.dart +++ b/test/nutrition/nutritional_plan_screen_test.dart @@ -64,6 +64,8 @@ void main() { mockIngredientRepo = MockIngredientRepository(); when(mockIngredientRepo.getById(any)).thenAnswer((_) async => null); + // NutritionNotifier.build() auto-fetches plans on first read. + when(mockNutritionRepo.fetchAllPlans()).thenAnswer((_) async => []); nutritionalPlanInfoResponse = jsonDecode( fixture('nutrition/nutritional_plan_info_detail_response.json'), diff --git a/test/nutrition/nutritional_plans_screen_test.dart b/test/nutrition/nutritional_plans_screen_test.dart index 8bb9ef7f3..9ae84590e 100644 --- a/test/nutrition/nutritional_plans_screen_test.dart +++ b/test/nutrition/nutritional_plans_screen_test.dart @@ -83,6 +83,8 @@ void main() { when(mockNutritionRepo.deletePlan(any)).thenAnswer( (_) async => http.Response('', 200), ); + // NutritionNotifier.build() auto-fetches plans on first read. + when(mockNutritionRepo.fetchAllPlans()).thenAnswer((_) async => []); container = ProviderContainer( overrides: [ diff --git a/test/routine/routines_provider_test.dart b/test/routine/routines_provider_test.dart index 84f9ffcb4..ec608b728 100644 --- a/test/routine/routines_provider_test.dart +++ b/test/routine/routines_provider_test.dart @@ -51,8 +51,14 @@ void main() { overrides: [routinesRepositoryProvider.overrideWithValue(mockRepo)], ); - // Act + // Drain the microtask queue so the build()-time auto-fetch lands + // *before* we exercise the explicit call below. Then clear interactions + // so the assertion only counts the call we're testing. final notifier = container.read(routinesRiverpodProvider.notifier); + await Future.delayed(Duration.zero); + clearInteractions(mockRepo); + + // Act await notifier.fetchAllRoutinesSparse(); // Assert