【发布时间】:2021-07-16 20:15:38
【问题描述】:
我正在尝试使用 PHP 和 MySQL 数据库在 Flutter 中运行一个基本的 API 调用,但我收到一条错误消息:
E/flutter ( 7461): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform
我的尝试
我已经尝试过这里提出的解决方案:Bad state: Insecure HTTP is not allowed by platform:
这里:https://www.programmersought.com/article/49295775092/
示例代码
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'),
)
);
}
}
URI前缀来自于env.sample.dart:
class Env{
static String URL_PREFIX = "http://[my.IP.address.here]/flutter_crud";
}
在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>
在 main/AndroidManifest.xml 文件中:
<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>
最后,我也在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>
到目前为止,还没有解决这个错误,我完全没有想法。任何反馈都会很棒!
【问题讨论】:
-
更新:我也试过运行
flutter clean,没有成功。 -
你解决了吗?
-
@abed 不,不幸的是没有!