Skip to content

Commit

Permalink
Add workaround for #722
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandgeider committed Jan 22, 2025
1 parent f77fbca commit eac2397
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
12 changes: 11 additions & 1 deletion lib/models/exercises/exercise.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import 'dart:developer';

import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:wger/helpers/consts.dart';
Expand Down Expand Up @@ -160,7 +162,15 @@ class Exercise extends Equatable {
equipment = baseData.equipment;
category = baseData.category;
translations = baseData.translations.map((e) {
e.language = languages.firstWhere((l) => l.id == e.languageId);
e.language = languages.firstWhere(
(l) => l.id == e.languageId,

// workaround for https://github.com/wger-project/flutter/issues/722
orElse: () {
log('Could not find language for translation ${e.languageId}');
return Language(id: e.languageId, shortName: 'unknown', fullName: 'unknown');
},
);
return e;
}).toList();
videos = baseData.videos;
Expand Down
14 changes: 11 additions & 3 deletions lib/providers/exercises.dart
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,17 @@ class ExercisesProvider with ChangeNotifier {
int exerciseId,
) async {
Exercise exercise;
final exerciseDb = await (database.select(database.exercises)
..where((e) => e.id.equals(exerciseId)))
.getSingleOrNull();

// TODO: this should be a .getSingleOrNull()!!! However, for some reason there
// can be duplicates in the db. Perhaps a race condition so that two
// entries are written at the same time or something?
final exerciseResult =
await (database.select(database.exercises)..where((e) => e.id.equals(exerciseId))).get();

ExerciseTable? exerciseDb;
if (exerciseResult.isNotEmpty) {
exerciseDb = exerciseResult.first;
}

// Exercise is already known locally
if (exerciseDb != null) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1576,4 +1576,4 @@ packages:
version: "3.1.2"
sdks:
dart: ">=3.6.0 <4.0.0"
flutter: ">=3.24.0"
flutter: ">=3.27.0"

0 comments on commit eac2397

Please sign in to comment.