'Flutter web view doesn't work on release mode
I'm creating a Flutter application.
If I run this application on debug mode webview works well
but if I run this application on release mode it shows blank screen. It's showing PlatformException
I tried to get help from github issues but couldn't find the solution.
Manifest
<uses-permission android:name="android.permission.INTERNET"/>
build.gradle
minSdkVersion 20
targetSdkVersion 29
pubspec.yaml
webview_flutter: ^1.0.7
Error Log
E/flutter (16872): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: Trying to create a platform view of unregistered type: plugins.flutter.io/webview
E/flutter (16872): at io.flutter.plugin.platform.j$a.g(Unknown Source:229)
E/flutter (16872): at io.flutter.embedding.engine.i.j$a.b(Unknown Source:152)
E/flutter (16872): at io.flutter.embedding.engine.i.j$a.j(Unknown Source:144)
E/flutter (16872): at e.a.c.a.j$a.a(Unknown Source:17)
E/flutter (16872): at io.flutter.embedding.engine.e.b.d(Unknown Source:57)
E/flutter (16872): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(Unknown Source:4)
E/flutter (16872): at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter (16872): at android.os.MessageQueue.next(MessageQueue.java:335)
E/flutter (16872): at android.os.Looper.loop(Looper.java:183)
E/flutter (16872): at android.app.ActivityThread.main(ActivityThread.java:7660)
E/flutter (16872): at java.lang.reflect.Method.invoke(Native Method)
E/flutter (16872): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/flutter (16872): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/flutter (16872): , null, null)
E/flutter (16872): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:581)
E/flutter (16872): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:158)
E/flutter (16872): <asynchronous suspension>
E/flutter (16872): #2 TextureAndroidViewController._sendCreateMessage (package:flutter/src/services/platform_views.dart:1036)
E/flutter (16872): <asynchronous suspension>
E/flutter (16872): #3 AndroidViewController.create (package:flutter/src/services/platform_views.dart:742)
E/flutter (16872): <asynchronous suspension>
E/flutter (16872): #4 RenderAndroidView._sizePlatformView (package:flutter/src/rendering/platform_view.dart:195)
E/flutter (16872): <asynchronous suspension>
Solution 1:[1]
Please check for "android.permission.INTERNET" pemission in your Manifest file. In "app/src/main/AndroidManifest.xml" folder. I use flutter_inappwebview: ^5.3.2 plugin and faced same issue in release mode.
Solution 2:[2]
I believe I ran into this same issue and what was happening was that there was both a cached flutter engine that had been initialised as well as a new flutter engine created later.
From that I went with a cached flutter engine and used a method channel to navigate as required.
So at some point somewhere(probably in your MainActivity or iOS equivalent) setup your Cached Flutter Engine:
val flutterEngine = FlutterEngine(this)
flutterEngine.dartExecutor.executeDartEntrypoint(
DartExecutor.DartEntrypoint.createDefault()
)
FlutterEngineCache
.getInstance()
.put(INSERT_YOUR_DESIRED_FLUTTER_ENGINE_ID_HERE, flutterEngine)
Then when you want to open your FlutterFragment(Android example):
var flutterEngine = FlutterEngineCache
.getInstance()
.get(INSERT_YOUR_FLUTTER_ENGINE_ID_FROM_BEFORE_HERE)
var flutterFragment = FlutterFragment
.withCachedEngine(INSERT_YOUR_FLUTTER_ENGINE_ID_FROM_BEFORE_HERE)
.build()
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
INSERT_YOUR_DESIRED_METHOD_CHANNEL_NAME_HERE
).invokeMethod("setRoute", INSERT_ROUTE_YOU_WANT_HERE)
openFragment(flutterFragment!!)
And an example of how you might use this method channel in your Flutter Dart code
class _MyViewState extends State<MyView> {
static const myMethodChannel = const MethodChannel(INSERT_YOUR_DESIRED_METHOD_CHANNEL_NAME_HERE);
Future<void> _handleMethodCalls(MethodCall call) async {
switch (call.method) {
case "setRoute":
Navigator.pushReplacmentNamed(context, INSERT_YOUR_DESIRED_ROUTE_NAME_HERE)
...
}
}
...
@override
void initState() {
super.initState();
platform.setMethodCallHandler(_handleMethodCalls);
}
}
Solution 3:[3]
I think you did not give internet permission in android/app/src/main/AndroidManifest.xml
add this line into the <manifest of android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
and more information for Flutter Insecure http is not allowed by platform you can see this discussion
<uses-permission android:name="android.permission.INTERNET" /> //This line add
<application
android:name="io.demo.app.test"
android:label="ProjectName"
android:usesCleartextTraffic="true" //This line add
android:icon="@mipmap/ic_launcher">
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Niyaz Ginatullin |
Solution 2 | CLL |
Solution 3 | Gunjon Roy |