15

Is there anything like componentWillDisapear() in RN?

Details of my issue:

I have two screens: Home and Details. My application uses 'firebase_admob' for adverts and the 'firebase_admob' (see the screenshot below) can render only fixed banner for the whole application. So when you go to other screens you see it too, but I need it only at Home screen. It means, when user navigates to Details I have to hide it. How to do it?

Note: I want to call dispose() when a user leaves the Home screen. To render the banner you need to call show() in BannerAd class and when you need to hide you just call dispose.

enter image description here

2
  • 1
    Sorry you say When you go to other screens you see it too but also I need this banner only at Home screen. Can you clarify if you want the banner on all pages or just the Home page?
    – soupjake
    Jan 22, 2019 at 15:28
  • I think you have the wrong title for this qn! The Home screen is still "rendered", even if there's another screen on top of it. I came here, because I want to find out when a screen is fully rendered, so that I don't try to push a replacement while the first one is still in the process of rendering... Your title seemed relevant to this issue, but the qn clearly isn't. 😏 Perhaps you should change the title to "is visible" instead of "is rendered". Oct 30, 2022 at 7:26

2 Answers 2

36

The code bellow allows to check if the page is visible for a user:

import 'package:flutter/material.dart';

class TestWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print('TestWidget: ${ModalRoute.of(context).isCurrent}');
    return Container();
  }
}

And this sample allows to check when a route is changed. But you can't get current page name.

1
  • 2
    You can't use this code in the background(other pages).It will throws " Looking up a deactivated widget's ancestor is unsafe."
    – BuffK
    Dec 14, 2019 at 12:00
7

As an addition to Kostya Vyrodov's answer and in response to his comment 'But you can't get current page name.', the following code snippet will print the current page name.

print("context.widget.toStringShort: ${context.widget.toStringShort()}");

Typically, this is best used in the page's initState to save the page name to a global variable for use elsewhere in the app. For example, I use it with Firebase Cloud Messaging in the _firebaseMessaging.configure() method to determine specific navigation requirements. If someone finds this useful, please up-vote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.