-1

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),
              );

3 Answers3

0

You need to call method like

onPressed: () => reloadController.reload()

Also you can try without anonymous method.

 onPressed: reloadController.reload,
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
0

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)),
Esmaeil Ahmadipour
  • 840
  • 1
  • 11
  • 32
0

thanks for all answers. I solved it doing this:

               IconButton(
                            onPressed: () {
                              setState(() {
                                () => WebViewController()..reload();
                              });
                            },
                            icon: const Icon(Icons.refresh),
                          ),
  • (`..reload()` looks fishy) – greybeard Feb 23 '23 at 16:34
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Feb 27 '23 at 00:14