Skip to content

Commit

Permalink
put initState code in didChangeDependencies
Browse files Browse the repository at this point in the history
- because initState doesn't have access to the build context
  • Loading branch information
w-ensink committed Jan 30, 2025
1 parent 04c24f2 commit 3d5b5cc
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions lib/src/screens/activity/widgets/recent_activity.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

import '../../../models/irma_configuration.dart';
import '../../../models/log_entry.dart';
Expand Down Expand Up @@ -31,26 +32,21 @@ class _RecentActivityState extends State<RecentActivity> {
late StreamSubscription _repoStateSubscription;

@override
void initState() {
super.initState();
//Delay to make build context available
Future.delayed(Duration.zero).then((_) async {
void didChangeDependencies() {
super.didChangeDependencies();
// only init the _historyRepo once by trying to access it and initializing when a late init error was thrown
// we do this because we can't access the build context in initState() and we only want to do it once
try {
_historyRepo;
} catch (_) {
_loadLogs();
if (!mounted) {
return;
}
_historyRepo = HistoryRepository(IrmaRepositoryProvider.of(context));
_repoStateSubscription = IrmaRepositoryProvider.of(context).getEvents().listen((event) {
if (event is SuccessSessionEvent) {
_loadLogs();
}
});
});
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
_historyRepo = HistoryRepository(IrmaRepositoryProvider.of(context));
}
}

@override
Expand Down

0 comments on commit 3d5b5cc

Please sign in to comment.