Skip to content

Commit

Permalink
resolves #486
Browse files Browse the repository at this point in the history
  • Loading branch information
svsk417 committed Mar 7, 2022
1 parent 6b8505f commit 46691a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
17 changes: 13 additions & 4 deletions package/lib/src/beamer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Beamer extends StatefulWidget {
/// A [State] for [Beamer].
class BeamerState extends State<Beamer> {
/// 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;
Expand All @@ -69,17 +69,26 @@ class BeamerState extends State<Beamer> {
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);
Expand Down
10 changes: 8 additions & 2 deletions package/lib/src/beamer_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ class BeamerDelegate extends RouterDelegate<RouteInformation>
/// `*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;
Expand Down Expand Up @@ -398,7 +404,7 @@ class BeamerDelegate extends RouterDelegate<RouteInformation>

// 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) {
Expand Down

0 comments on commit 46691a6

Please sign in to comment.