Skip to content

Commit

Permalink
fix: cannot use ref before provider rebuilt
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesvedder committed Jun 18, 2024
1 parent 7fc8deb commit 89bba68
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
8 changes: 5 additions & 3 deletions designer_v2/lib/features/study/study_test_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ part 'study_test_controller.g.dart';
class StudyTestController extends _$StudyTestController {
@override
StudyTestControllerState build(StudyID studyId) {
// final studyRepo = ref.read(studyRepositoryProvider);
ref.onDispose(() {
// Reload the study after disposing the test controller so that any
// data changes resulting from testing are reflected in the study for
// other parts of the app (e.g. test data that was generated)
//
// Ideally, we would stream changes from the backend/database directly,
// but this is a sufficient workaround for now
ref.watch(studyRepositoryProvider).fetch(studyId);
// but this is a sufficient workaround for now.

// TODO test if this is needed or the study gets already updated after closing the test view
// studyRepo.fetch(studyId);
});
return StudyTestControllerState(
studyId: studyId,
Expand All @@ -40,7 +43,6 @@ class StudyTestController extends _$StudyTestController {
}

/// Provide a controller parametrized by [StudyID]
@riverpod
PlatformController studyTestPlatformController(
StudyTestPlatformControllerRef ref,
Expand Down
17 changes: 5 additions & 12 deletions designer_v2/lib/features/study/study_test_frame.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,15 @@ class PreviewFrame extends ConsumerStatefulWidget {
class _PreviewFrameState extends ConsumerState<PreviewFrame> {
PlatformController? frameController;

@override
void initState() {
super.initState();
runAsync(() => _subscribeStudyChanges());
}

@override
void didUpdateWidget(PreviewFrame oldWidget) {
if (mounted) runAsync(() => _subscribeStudyChanges());
super.didUpdateWidget(oldWidget);
}

void _subscribeStudyChanges() {
final formViewModelCurrent =
ref.read(studyFormViewModelProvider(widget.studyId));
runAsync(() => _subscribeStudyChanges(formViewModelCurrent));
}

void _subscribeStudyChanges(StudyFormViewModel formViewModelCurrent) {
formViewModelCurrent.form.valueChanges.listen((event) {
if (frameController != null) {
final formJson =
Expand Down Expand Up @@ -92,9 +85,9 @@ class _PreviewFrameState extends ConsumerState<PreviewFrame> {
final state = ref.watch(studyTestControllerProvider(widget.studyId));
final formViewModel = ref.watch(studyTestValidatorProvider(widget.studyId));

// Rebuild iframe component & url
// Rebuild iframe component and url
frameController =
ref.read(studyTestPlatformControllerProvider(widget.studyId));
ref.watch(studyTestPlatformControllerProvider(widget.studyId));
_updatePreviewRoute();
frameController!.activate();
frameController!.listen();
Expand Down

0 comments on commit 89bba68

Please sign in to comment.