I need to update webview's url with my setState. How to do that?

The initial url I haved set on constructor have been loaded successfully. But it's not working when I try load the new one through my drawer menu by onTap method.

...
class _MyWebState extends State<MyWeb> {
  Completer<WebViewController> _controller = Completer<WebViewController>();
  final Set<String> _favorites = Set<String>();
  String url;
  Widget web;

  _MyWebState() {
    web = WebView(
      initialUrl: 'https://my.flazhost.com',
      onWebViewCreated: (WebViewController webViewController) {
        _controller.complete(webViewController);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('MyWeb'),
        actions: <Widget>[
          NavigationControls(_controller.future),
          Menu(_controller.future, () => _favorites),
        ],
      ),
      body: web,
      drawer: Drawer(
        child: ListView(
          children: <Widget>[
            ListTile(
              leading: FlutterLogo(
                size: 20,
              ),
              title: Text('Web AlQudwah'),
              onTap: () {
                setState(() {
                  web = WebView(
                    initialUrl: 'https://flazhost.com',
                    onWebViewCreated: (WebViewController webViewController) {
                      _controller.complete(webViewController);
                    },
                  );
                  Navigator.pop(context);
                });
              },
            ),
          ],
        ),
      ),
      floatingActionButton: _bookmarkButton(),
    );
  }
}
...

How to update webview's url onTap method? Please help?

0 Answers