fluttercommunity / fluttercommunity/flutter_webview_plugin

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

Aperta
#543 8 commenti 0 reazioni 0 assegnatari Vedi su GitHub
bug
Lingua principale
Java
Stelle
1.5k
Fork
938
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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](https://github.com/fluttercommunity/flutter_webview_plugin/issues/30#issuecomment-437330743) 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 _onUrlChanged;
StreamSubscription _onHttpError;
StreamSubscription _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);
}
});
}
});
}
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.