Skip to content

Commit

Permalink
fix issue on changing screen after adding a recipe to an empty daily …
Browse files Browse the repository at this point in the history
…menu
  • Loading branch information
andreacioni committed Nov 3, 2024
1 parent 812931c commit 68f2935
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import 'package:common/utils.dart';
import 'package:collection/collection.dart';
import 'package:weekly_menu_app/providers/user_preferences.dart';
import 'package:flutter_data/flutter_data.dart' hide Repository;
import 'package:weekly_menu_app/widgets/shared/shimmer.dart';

import '../../shared/flutter_data_state_builder.dart';
import '../recipe_screen/screen.dart';
Expand Down Expand Up @@ -51,18 +50,12 @@ class DailyMenuSectionStreamWrapper extends HookConsumerWidget {
return RepositoryStreamBuilder<DailyMenu>(
key: ValueKey(date.formatId()),
stream: stream,
loading: NewDailyMenuNotifierWrapper(
date,
key: Key(date.formatId()),
),
loading: buildLoading(),
errorBuilder: (context, error) {
if (error != null) {
// in case we have a 404 just place an empty DailyMenuSection
if (error is DataException && error.statusCode == 404) {
return NewDailyMenuNotifierWrapper(
date,
key: Key(date.formatId()),
);
return buildLoading();
}

if (error is OfflineException) {
Expand All @@ -80,6 +73,13 @@ class DailyMenuSectionStreamWrapper extends HookConsumerWidget {
},
);
}

Widget buildLoading() {
return NewDailyMenuNotifierWrapper(
date,
key: Key(date.formatId()),
);
}
}

class NewDailyMenuNotifierWrapper extends HookConsumerWidget {
Expand Down
7 changes: 6 additions & 1 deletion packages/data/lib/flutter_data/daily_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ mixin DailyMenuAdapter<T extends DataModelMixin<FlutterDataDailyMenu>>
}

if (err.statusCode == 404) {
return null;
// in case a daily menu is not found we create a new one
// returning null here caused an issue with flutter_data because new item,
// once added to a menu, were not displayed while changing screen.
// A restart fixed the issue but that could not be the solution.
return FlutterDataDailyMenu(
idx: null, date: Date.parse(dateIdFormat, id as String), meals: {});
}

return originalOnError?.call(err, label, adapter);
Expand Down

0 comments on commit 68f2935

Please sign in to comment.