Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web view is too slow

Tags:

flutter

Web view takes more than 4 sec to load the webpage. The same page takes less than 2 sec in Native app. Is there a way to speedup the load time. I tried both Official webview_flutter and flutter_webview_plugin.

like image 423
Sugumar Avatar asked Apr 09 '19 08:04

Sugumar


2 Answers

If you app relies on WebView, just choose other tools: Swift for iOS & Kotlin for Android.

Here is why:

  1. WebView actually does not load pages slow. Instead, creating the WebView widget is slow;

  2. In order to solve 1, you might want use a cached WebView. Unfortunately, that is not easy. Layout changes (e.g. animation) might trigger a WebView "recreating" (the cached WebView becomes invalid/staled). And the "recreating" is very slow;

  3. Flutter's widgets depend on "state" outside of the widgets, and widgets' creating are supposed to be fast/simple. Unfortunately, WebView (which is not a native widget) is not the case. WebView has its complex internal "state", a recreation simple discard everything and you returns to the WebView's initial state (initial URL). And it is very slow (Creating time + LoadTime: Network overhead);

  4. It is very hard to create a "external state" outside a WebView, therefore after a WebView's recreating it cannot resume from the external state;

  5. Since WebView's recreating is very slow, it totally kills animation and gives user a very bad experience. A solution might be put a WebView as your main page and never try to animate to a new WebView (just like a Wiki App Demo in YouTube).

Conclusion:

So, now, WebView in flutter is not ready and please don't consider use it seriously.

Discussion:

Flutter's widgets design is quite "unusual" since they are basically immutable. States outside widgets (external state) are used. When state changes, instead of modify the widget, Flutter choose to create a new widget based on the new state. Therefore, widgets are deigned to be light weighted so they can be created/destroyed very quickly. Unfortunately, WebView cannot fall into this category. WebView is as complex as the whole Flutter framework, so it cannot be a native widget but a plug-in. And WebView has its own internal state which is not compatible with the framework, which results in keep on being destroyed/recreated by the framework.

I am not sure why Flutter's widgets are designed in this way, maybe it is easier/faster for creating the framework? I saw some complex examples (~100 lines) using Redux/BLOC/Steam just in order to "change" a widget, which might just need a one-line of code in other frameworks.

Performance is also an issue. Rebuild a complex widgets tree is slow. Then you need writing a lot of code (Redux/BLOC/Stream/ScoppedModel...) in order to implement a partial widgets tree build.

Even for a very simple app, performance of Flutter is still not as good as native (https://thoughtbot.com/blog/examining-performance-differences-between-native-flutter-and-react-native-mobile-development). In fact, I'd like considering Flutter as "native" since it is compiled into machine code instead of Java's ByteCode.

Finally:

I am a new Flutter learner and start playing with Flutter for a couple of weeks. The widgets framework and the WebView plug-in just made me headache. A lot of time spent on the UI interface instead of the core logic of my app.

I am not saying Flutter is not good. Actually, I think it is the best cross-platform framework for iOS/Android. It just might be something (e.g. complex external widget like WebView) was not being taken into consideration while the framework was being designed. Hope the Flutter team can find a solution for this, maybe a special case for handling a complex external plug-in?

I will keep on learning/playing with Flutter.

like image 54
user1717750 Avatar answered Oct 21 '22 01:10

user1717750


And now Hybrid-Composition is default in webview_flutter 3.0.0: https://pub.dev/packages/webview_flutter/changelog

Just tried it on my side and since it's not perfect, it's much more faster

cheers!

like image 1
Bertrand Gélinas Avatar answered Oct 21 '22 02:10

Bertrand Gélinas