Skip to content

OnUrlChange/onStateChange STOP tracking when moving from one webview to another webview instance in same app #543

Open
@feudadrien

Description

@feudadrien

I Love this Plugin. I got everything I needed but one little issue. It may be a quick fix for you :)

I saw this issue description from @l-k22 in issue and thought it was well worded for my case(already developed but stuck) with some changes in bold as below

I can get the scaffold to work with a bottom navigation bar when it is returned directly in my build method. However, I have made some alterations to my app so I now have a

listview of cards which each lead to a different website. When the user clicks on card it opens the webview and I wrote an onUrlChange so that if a website brings them to an unsupported url_Scheme (like market:// or intent://) it will bring them out to URL launcher-like plugin.

Observation: It works when the first card is chosen and an unsupported link is clicked like to go to AppStore and play store

Issue: when a user goes out of webview card select and goes to another card (or even comes back to the same card) it doesn't work. Upon looking at the console, onUrlChange/OnStateChange both stop functioning when leaving webview :(

 StreamSubscription _onDestroy;
 StreamSubscription<String> _onUrlChanged;
 StreamSubscription<WebViewHttpError> _onHttpError;
 StreamSubscription<WebViewStateChanged> _onStateChanged;

 @override
 void dispose() {
   _onUrlChanged.cancel();
   _onHttpError.cancel();
   _onDestroy.cancel();
   _onStateChanged.cancel();
   flutterWebViewPlugin.dispose();
   super.dispose();
 }

@override
 void initState() {
 super.initState();

flutterWebViewPlugin.close();

   // Add a listener to on destroy WebView, so you can make came actions.
   _onDestroy = flutterWebViewPlugin.onDestroy.listen((_) {
     print("destroy");
   });

   _onStateChanged =
       flutterWebViewPlugin.onStateChanged.listen((WebViewStateChanged state) {
         print("onStateChanged: ${state.type} ${state.url}");
       });

   _onUrlChanged = flutterWebViewPlugin.onUrlChanged.listen((String url) {
     print("OnUrlChange navigating to...$url");
     if (mounted) {
       setState(() {
         if (!(url.startsWith("www") || url.startsWith("https") ||
             url.startsWith("http") || !(url.contains("play.google.com")) ||
             !(url.contains("apps.apple.com")))) {
           FlutterWebBrowser.openWebPage(url: url,
               androidToolbarColor: Colors.redAccent);
         }
       });
     }
     });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions