0

I'm using flutter 2.0.0 and downgraded it from flutter 2.2.3 but still unable to resolve the issue.

I have already concerned the following:

And of course the official docs here as well.

So, I'm trying to access a local host at my Azure VM with URL like http://my-ip:5000. I have already done the following steps but still unable to access it.

NOTE: Its working fine when I test it with POSTMAN

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cui.adam">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    

   <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="ADAM"
        android:usesCleartextTraffic="true"
        android:icon="@mipmap/ic_launcher">
        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
        </receiver>
        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
              <action android:name="FLUTTER_NOTIFICATION_CLICK" />
              <category android:name="android.intent.category.DEFAULT" />
            </intent-filter> 
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <meta-data android:name="io.flutter.network-policy"
              android:resource="@xml/network_security_config"/>
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

network_system_config.xml

<?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>

Flutter Code

CustomButton(
  btnWidth: 100.0,
  btnHeight: 45.0,
  btnOnPressed: () async {
    if (_formKey.currentState.validate()) {
      setState(() {
        _isWorking = true;
      });
      String url =
          "http://my-IP:5000/end-point";

      var body = {
        "username": "someUsername",
        "password": "somePassword",
        "targeted_username": "someUsername",
      };
      var headers = {
        "Accept": "application/json",
        "Access-Control-Allow-Origin": "*"
      };
      http.Response response = await http
          .post(
        Uri.parse(url),
        body: body,
        headers: headers,
      )
          .whenComplete(() {
        setState(() {
          _isWorking = false;
        });
      });
      print(response.body);
      if (response.statusCode == 200) {
        setState(() {
          _dataScraped = !_dataScraped;
        });
        print(response.body);
      } else {
        print("SOME ERROR!!");
      }
    }
  },
  btnColor: kLightGreenColor,
  btnText: _isWorking
      ? kLoaderWhite
      : Text(
          "Get Data",
          style: kBtnTextStyle,
        ),
),

Flutter doctor

[√] Flutter (Channel beta, 2.0.0, on Microsoft Windows [Version 10.0.19043.1165], locale en-US)
    • Flutter version 2.0.0 at D:\flutter2
    • Framework revision 60bd88df91 (6 months ago), 2021-03-03 09:13:17 -0800
    • Engine revision 40441def69
    • Dart version 2.12.0

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at C:\Users\Hamza\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 4.1.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] VS Code (version 1.59.1)
    • VS Code at C:\Users\Hamza\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.25.0

[√] Connected device (3 available)
    • CPH1909 (mobile) • RGMFGAFIYDW8AQ5S • android-arm64  • Android 8.1.0 (API 27)
    • Chrome (web)     • chrome           • web-javascript • Google Chrome 92.0.4515.159
    • Edge (web)       • edge             • web-javascript • Microsoft Edge 92.0.902.84

• No issues found!
Hamza
  • 1,523
  • 15
  • 33

1 Answers1

0

You have created network config but haven't mentioned it in Manifest! Add it like this,

<application
  android:networkSecurityConfig="@xml/network_system_config">
  
  <!--Other Stuff In Between-->

</application>

Remove cleartextTrafficPermitted from Manifest because you already have it in network config.

And if these don't work then add <domain> to your network config like this,

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">http://mywebsite.xyz</domain>
        <domain includeSubdomains="true">http://mydatabaseurl.abc</domain>
    </domain-config>
</network-security-config>
Detained Developer
  • 1,134
  • 2
  • 13
  • 33
  • I did the above step, but they are only working with `GET` not with `POST`. Again, the `GET` and `POST` are working fine with Postman – Hamza Sep 02 '21 at 06:52