Reputation: 303
I'm trying to run a basic API call in Flutter using PHP and a MySQL database, but I'm receiving an error which reads:
E/flutter ( 7461): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform
What I've Tried
I have tried the solution proposed here: Bad state: Insecure HTTP is not allowed by platform:
and here: https://www.programmersought.com/article/49295775092/
Sample Code
home.dart
import 'package:flutter/material.dart';
import 'package:flutter_crud/env.sample.dart';
import 'dart:convert';
import 'package:http/http.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
void getData() async{
Response response = await get(Uri.parse("${Env.URL_PREFIX}/list"));
Map data = jsonDecode(response.body);
print(data);
}
@override
void initState() {
super.initState();
getData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter CRUD API'),
centerTitle: true,
),
body:Center(
child: Text('Hello'),
)
);
}
}
The URI prefix comes from env.sample.dart:
class Env{
static String URL_PREFIX = "http://[my.IP.address.here]/flutter_crud";
}
In the debug/AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jacobcollinsdev.flutter_crud">
<uses-permission android:name="android.permission.INTERNET"/>
<application android:usesCleartextTraffic="true"/>
</manifest>
In the main/AndroidManifest.xml file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jacobcollinsdev.flutter_crud">
<uses-permission android:name="android.permission.INTERNET" /> <------- I added this line
<application
android:label="flutter_crud"
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"> <------ I added this line
<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">
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<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>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<uses-library
android:name="org.apache.http.legacy"
android:required="false" /> <-------I added this code block
</application>
</manifest>
Finally, I have also created this file in android/app/main/res/xml/network_security_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>
So far nothing has resolved the error and I'm all out of ideas. Any feedback would be grand!
Upvotes: 3
Views: 4243
Reputation: 1121
You should try loading your images from a SSL certificate domain. That is using HTTPS
rather than HTTP
.
So for your case, replace "http://[my.IP.address.here]/flutter_crud"
to use https://
.
Upvotes: 0
Reputation: 1608
simply these lines in
android/app/src/main/AndroidManifest.xml
1). Add
<uses-permission android:name="android.permission.INTERNET" />
beforeapplication
tag
2). Add
android:usesCleartextTraffic="true"
insideapplication
tag
Upvotes: 2