Home > database >  Reload a webview
Reload a webview

Time:01-21

I'm trying to reload a WebView page when I click a button on the Appbar, using the webview_flutter package. For that I'm using a WebviewController, but I'm not getting it... When I click on the button, nothing happens. Check the WebViewCrontroller widget:

WebViewController reloadController = WebViewController()..reload();

My Scaffold widget:

return Scaffold(
                appBar: AppBar(
                  title: Text(utf8decoder.convert(widget.title.codeUnits)),
                  backgroundColor: hexToColor(user!.templateColorPrimary),
                  actions: [
                    IconButton(
                      onPressed: () => reloadController,
                      icon: const Icon(Icons.refresh),
                    )
                  ],
                ),
                body: WebViewWidget(controller: widgetController),
              );

CodePudding user response:

You need to call method like

onPressed: () => reloadController.reload()

Also you can try without anonymous method.

 onPressed: reloadController.reload,

CodePudding user response:

at first , modify WebViewController like below:

  final WebViewController webViewController;

Pay attention to this point, you need 1 controller and you don't need to write a separate controller for reload.

modify below line in your code :

        body: WebViewWidget(
              controller: 
           // widgetController    <---remove this
              webViewController   // add this
          ),

and then for using :

   IconButton(
      onPressed: () => webViewController.reload(),
      icon: const Icon(Icons.refresh)),
  • Related