69

I have implemented webView in flutter but it is not opening my php website which is on server what I'm doing wrong.

I am new to flutter and tried webview to integrate my website webpage in my application but no luck.

Widget build(BuildContext context) {
    // TODO: implement build
    return WebviewScaffold(
      appBar: AppBar(iconTheme:IconThemeData(color: Colors.white),title: Text("Intake Form",style:new TextStyle(color: Colors.white,fontWeight: FontWeight.bold)),backgroundColor:Colors.indigoAccent,automaticallyImplyLeading: false),
     url: url,
      //url: "http://xxxxxxxx/",
       withJavascript: true,
       supportMultipleWindows: true,
      withLocalStorage: true,
      allowFileURLs: true,
      enableAppScheme: true,
      appCacheEnabled: true,
      hidden: false,
      scrollBar: true,
      geolocationEnabled: false,
      clearCookies: true,
       // usesCleartextTraffic="true"



    );
  }

I expect the output as running webview but error is thrown.

0

6 Answers 6

145

In main directory of your Flutter project you have three main folders:

- lib         =  your Dart code
- ios         =  generated structure for iOS platform
- android     =  generated structure for Android platform

We are interested in android directory. When you open it, you will see "typical Android app structure".

So you have to do 2 things:

1) Add new file in res

Go to directory:

my_flutter_project/android/app/src/main/res/

Create xml directory (in res!)

And inside xml add new file with name: network_security_config.xml and content:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
            <certificates src="user" />
        </trust-anchors>
    </base-config>
</network-security-config>

network_security_config.xml should be located in path:

my_flutter_project/android/app/src/main/res/xml/network_security_config.xml

network_security_config

Here you can find more information about this file:

https://developer.android.com/training/articles/security-config

2) Modify AndroidManifest.xml

Go to:

flutter_project/android/app/src/main/AndroidManifest.xml

enter image description here

AndroidManifest.xml is XML file, with structure:

<manifest>
    <application>
        <activity>
            ...
        </activity>
        <meta-data >
    </application>
</manifest>

So for <application> PROPERTIES you have to add 1 line:

android:networkSecurityConfig="@xml/network_security_config"

enter image description here

Remember that you have to add it as property (inside application opening tag):

<application

    SOMEWHERE HERE IS OK

>

Not as a tag:

<application>           <--- opening tag

    HERE IS WRONG!!!!

<application/>          <--- closing tag
10
  • 25
    What a wonderful answer with every step explained with complete clarity!
    – DroidOS
    Commented Apr 6, 2020 at 14:16
  • 3
    I can't understand why topic starter hasn't accepted this answer. It really works! Thanks, and take my upvote
    – Konstantin
    Commented Oct 7, 2020 at 5:06
  • 3
    The answer is correct here is my perception according to the question is that when a developer implement an site with http:// then this security file is needed else if you just change the url with https:// you will not get any error in the web view Commented Dec 30, 2020 at 14:40
  • 1
    it is wonderful example and explained simple and very easy working method, thank you.
    – ViKi Vyas
    Commented Mar 21, 2022 at 10:53
  • 1
    this is most elaborate answer ever!!! thanks Commented Apr 1, 2023 at 6:28
92

set usesCleartextTraffic property to true in your AndroidManifest file, like below.

<application
....
android:usesCleartextTraffic="true"
....>
5
  • 7
    Even with this option, it doesn't work :/ I had to run flutter clean and then rerun the project again
    – Ionel Lupu
    Commented Aug 1, 2019 at 12:49
  • 3
    @lonelLupu Possible no need for cleaning. Seems that stop the whole app and rebuild it is enough. The reason is that, the AndroidManifest does not reload with Flutter's hot reload/restart.
    – ch271828n
    Commented Oct 12, 2020 at 3:20
  • Simple and very easy working method.
    – ViKi Vyas
    Commented Mar 21, 2022 at 10:51
  • I personally think setting this to true goes against Android best practices related network security; developer.android.com/guide/topics/manifest/application-element . From Android 8 this property is false by default.
    – user7209975
    Commented Jun 1, 2022 at 7:56
  • not working after flutter clean and rerun the app.
    – eastwater
    Commented Apr 28 at 6:31
30

Android:

In flutter_project/android/app/src/main/AndroidManifest.xml

Add

<application
....
android:usesCleartextTraffic="true"
....>

IOS:

In flutter_project/ios/Runner/info.plist

Add

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Run flutter clean.Then, run the app.

0
4

For ios modify info.plist ./ios/Runner/info.plist

Add the following:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

and run flutter clean before spinning up the app again

1
  • make network_security_config.xml file.

<?xml version="1.0" encoding="utf-8"?>
        <network-security-config>
            <base-config cleartextTrafficPermitted="true">
                <trust-anchors>
                    <certificates src="system" />
                </trust-anchors>
            </base-config>
        </network-security-config>

enter image description here

  • add this two line in Manifest file in Application tag.
4
  • Where exactly this file needs to be placed?
    – akifquddus
    Commented Feb 7, 2020 at 21:58
  • in res folder make xml folder and then put this .xml file. Commented Feb 8, 2020 at 7:58
  • I am working in flutter, so there is no res folder in there.
    – akifquddus
    Commented Feb 8, 2020 at 13:02
  • According to docs: This flag is ignored on Android 7.0 (API level 24) and above if an Android Network Security Config is present. Commented Nov 23, 2022 at 15:02
0

Implemented webView in Flutter, Try flutter_inappwebview: flutter_inappwebview: ^6.0.0 https://pub.dev/packages/flutter_inappwebview

Getting Started: https://inappwebview.dev/docs/intro

Requirements:

Dart sdk: ">=2.17.0 <4.0.0" Flutter: ">=3.0.0" Android: minSdkVersion >= 19, compileSdk >= 34, AGP version >= 7.3.0 (use Android Studio - Android Gradle plugin Upgrade Assistant for help), support for androidx (see AndroidX Migration to migrate an existing app) iOS 9.0+: --ios-language swift, Xcode version >= 14.3 MacOS 10.11+: Xcode version >= 14.3

Not the answer you're looking for? Browse other questions tagged or ask your own question.