dart - Flutter - How the NavigatorObserver class works? -
i'm trying detect when navigator performs basic operations (push , pop), found class achieve navigatorobserver class, can't find example on how works.
i tried implements interface:
class myclassstate extends state<myclass> navigatorobserver{ .... @override void didpop(route<dynamic> route, route<dynamic> previousroute) { print('this never called'); } @override void didpush(route<dynamic> route, route<dynamic> previousroute) { print('this never called'); } }
of course i'm calling navigator.of(context).push(...);
override methods, never called.
i suspect binding navigator missing, i'm not sure.
any suggestions?
thanks in advance!
edit:
i think want simple can't figure out how it.
i have page a pushes page b.
on page b have periodic timer must on there (on page b).
all want cancel timer timer.cancel()
when page b popped out (with button on android or button in appbar), since when page b popped out timer still executing if page b has gone.
- i can't pass timer param navigator.pop() since don't handle buttons (the 1 in appbar , android button).
edit 2:
-i found way solves problem, did cancel timer on
@override void dispose() { timer.cancel(); super.dispose(); }
method. i'm not sure if best way accomplish it.
note:
with workaround timer still executing if press home button or open multitask on android, , when go app restarted again timer still executing code.
most of time don't need implement navigatorobserver
. see this other stackoverflow answer explaining how use push
, pop
pass information between routes. in use case you've described, should addlistener
animationcontroller
, using tickerproviderstatemixin
obtain suitable vsync
object. ensures callback not fire when app suspended or after state
disposed. (instead of addlistener
, use animatedbuilder
or animatedwidget
primary purpose of callback rebuild section of widget tree.)
the main time when you'd want navigatorobserver
if you're using plugin firebase analytics. can see example usage in plugins repo. pass navigatorobserver
in navigatorobservers
argument materialapp
constructor:
static firebaseanalyticsobserver observer = new firebaseanalyticsobserver(analytics: analytics); ... return new materialapp( navigatorobservers: <navigatorobserver>[observer], ... );
it unusual have state
implements navigatorobserver
because materialapp
should near top of widget hierarchy. @ time you're constructing it, state
objects won't exist yet you'll have hard time putting them navigatorobservers
array. might instead use class isn't state
. if necessary, can use globalkey<myclassstate>
find state
needs notified (but if you're doing this, there might easier way accomplish want).
Comments
Post a Comment