'Excecute an action on a webcontroller from a Reusabledrawer

I have Screen1 with a webview, using webview_flutter, and I have a webcontroller. Screen1 also uses a side menu defined in a separate class, resuabledrawer():

class _Screen1 extends State<Screen1> {
WebViewController _controller;

Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Image.asset(
              'lib/assets/image.png')),
          drawer: ReusableDrawer(),
          body: Stack(
            children: [
               WebView(
                 initialUrl: 'https:www.address.com',
                  onWebViewCreated: (WebViewController c) {
                    _controller = c;
                  },
                  ...

The ReusableDrawer class (contained in a separate file to Screen1) has a list of routes away from Screen1 and utilises Navigator.pushReplacement to replace Screen1 with the new screen:

class ReusableDrawer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Theme( data: Theme.of(context).copyWith(canvasColor: Colors.black),
      child: Drawer(
        child: ListView(
          children: [
            Container(
             child: DrawerHeader(...),
              child: Text('Title'),
            ),
            ),
            CustomListTile(
              Icons.home,
              'Main menu',
              () => {
                  Navigator.pushReplacement(context, MaterialPageRoute(
                    builder: (context) => LoadingScreen())),
              },
            ),

I would like to clear the webcontroller cache on Screen1 when ReusableDrawer() does the pushReplacement. (i.e. I want to execute _controller.clearCache();). Obviously the _controller is only visible to Screen1 though.

Do I need to 'pass' the webcontroller to ReusableDrawer() somehow or, preferentially, is there alistener I can add to Screen1 so Screen1 knows it has been replaced (ie.. like pop) and will then execute _controller.clearCache()?

Thanks for any advice. (nb I do not want to clearcache on webcontroller create for reasons I will not go into here).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source