how to preload a flutter webview widget

1.3k views Asked by At

I'm using webview_flutter.

Is there anyway I can preload a webview widget when the app starts, and reuse the same widget multiple times in the app?

  WebView webview = buildWebview();

  Widget getWebview() {
    return webview;
  }

  Widget buildWebview() {
    return WebView(
      key: GlobalKey(),
      onWebViewCreated: (WebViewController webViewController) {
          print("WebView is Created");
      },
      ...
    );
  }

This is what I tried, but it seems the WebView is created when the widget is shown on the screen, not when the app starts. And when I call getWebview() again, WebView is created again.

2

There are 2 answers

0
GN Vageesh On

nice initiate and idea.

You can actually do this

In the main.dart file call this class. this will make the app start the app with your preferred webview every time the app is launched.

And for reusing the class, again and again, create another class and load this webview. Then call or import and use this class whenever needed

1
Ashok On

In case if you want to preload all your pages when the startup then you can use the indexed stack in the class where your TabBar is declared.

Wrap up the body and call all the respective pages there as mentioned below:

Widget build(BuildContext context) {
   return
    Scaffold(
      body:IndexedStack(
               index: _selectedIndex, children: _children,
             ),),}