sebarck
sebarck

Reputation: 13

Flutter "flutter_webview_plugin" Android back button problem

Im using the flutter plugin "flutter_webview_plugin" for open an URL inside my app. The problem, is when in Android, you press the back button, it "returns" and show the progress indicator infinitely on the WebViewScaffold until you press again the back button.

I tried adding this code within builder function:

    flutterWebviewPlugin.onDestroy.listen((_) {
        if (Navigator.canPop(context)) {
          Navigator.of(context).pop();
          flutterWebviewPlugin.dispose();
          flutterWebviewPlugin.cleanCookies();
        }
      });

It works for the "first" time i call my function. But, on the second, it happens again.

_launchURL() async {
    String url = result;
    if (await canLaunch(url)) {
      Navigator.of(context).push(MaterialPageRoute(
          builder: (ctx) => WebviewScaffold(
                clearCache: true,
                clearCookies: true,
                withLocalStorage: false,
                appCacheEnabled: false,
                initialChild: Center(child: CircularProgressIndicator()),
                url: url,
                appBar: AppBar(title: Text("Fichas DoggyStyle")),
              )));
      flutterWebviewPlugin.onDestroy.listen((_) {
        if (Navigator.canPop(context)) {
          Navigator.of(context).pop();
          flutterWebviewPlugin.dispose();
          flutterWebviewPlugin.cleanCookies();
        }
      });
    } else {
      throw 'No pudimos abrir la ficha :(';
    }
  }

Upvotes: 1

Views: 2432

Answers (1)

Sigma App Development
Sigma App Development

Reputation: 158

Try calling flutterWebviewPlugin.onDestroy.listen() inside the initstate() of a widget... it works fine for me...

Upvotes: 1

Related Questions