From 46691a60e8ac31348dea65b9770a0df3fe577649 Mon Sep 17 00:00:00 2001 From: svsk417 Date: Mon, 7 Mar 2022 19:08:33 +0100 Subject: [PATCH] resolves #486 --- package/lib/src/beamer.dart | 17 +++++++++++++---- package/lib/src/beamer_delegate.dart | 10 ++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/package/lib/src/beamer.dart b/package/lib/src/beamer.dart index e4c6670b..ac99ac26 100644 --- a/package/lib/src/beamer.dart +++ b/package/lib/src/beamer.dart @@ -57,7 +57,7 @@ class Beamer extends StatefulWidget { /// A [State] for [Beamer]. class BeamerState extends State { /// A parent Router of this Beamer / Router. - late Router parent; + late Router? parent; /// A getter for [BeamerDelegate] of the [Beamer] whose state is this. BeamerDelegate get routerDelegate => widget.routerDelegate; @@ -69,17 +69,26 @@ class BeamerState extends State { void didChangeDependencies() { super.didChangeDependencies(); parent = Router.of(context); - routerDelegate.parent = parent.routerDelegate as BeamerDelegate; + routerDelegate.parent = parent!.routerDelegate as BeamerDelegate; + } + + @override + void dispose() { + parent = null; + routerDelegate.parent = null; + super.dispose(); } @override Widget build(BuildContext context) { + // The parent will only be null, if this state is disposed and therefore + // `build` cannot be called on it any more. final backButtonDispatcher = widget.backButtonDispatcher ?? - ((parent.backButtonDispatcher is BeamerBackButtonDispatcher && + ((parent!.backButtonDispatcher is BeamerBackButtonDispatcher && widget.createBackButtonDispatcher) ? BeamerChildBackButtonDispatcher( parent: - parent.backButtonDispatcher! as BeamerBackButtonDispatcher, + parent!.backButtonDispatcher! as BeamerBackButtonDispatcher, delegate: routerDelegate, ) : null); diff --git a/package/lib/src/beamer_delegate.dart b/package/lib/src/beamer_delegate.dart index 2db29624..38ed3c87 100644 --- a/package/lib/src/beamer_delegate.dart +++ b/package/lib/src/beamer_delegate.dart @@ -74,7 +74,13 @@ class BeamerDelegate extends RouterDelegate /// `*App.router` and at least one more [Beamer] in the Widget tree. BeamerDelegate? get parent => _parent; set parent(BeamerDelegate? parent) { - if (parent == null || _parent == parent) { + if(parent == null && _parent != null) { + _parent!.removeListener(_updateFromParent); + _parent!._children.remove(this); + _parent = null; + return; + } + if (_parent == parent) { return; } _parent = parent; @@ -398,7 +404,7 @@ class BeamerDelegate extends RouterDelegate // run guards on _beamLocationCandidate final context = _context; - if (context != null && context is Element && (context as Element).dirty == false) { + if (context != null) { final didApply = _runGuards(context, _beamLocationCandidate); _didRunGuards = true; if (didApply) {