Flutter I/flatter(8876):捕获错误:错误状态:平台不允许不安全的HTTP:
我制作了一个使用世界时API的颤振应用程序。直到有一天我从我的github配置文件下载并试着运行它,它才开始正常工作。现在,它显示“错误:布尔表达式不能为空”。 所以,我更改了它的值并初始化了它。然后它没有相应地改变它的值。 然后,当我在控制台上打印响应正文时,它显示了错误状态错误:不允许http请求Flutter I/flatter(8876):捕获错误:错误状态:平台不允许不安全的HTTP:,flutter,github,mobile-development,Flutter,Github,Mobile Development,我制作了一个使用世界时API的颤振应用程序。直到有一天我从我的github配置文件下载并试着运行它,它才开始正常工作。现在,它显示“错误:布尔表达式不能为空”。 所以,我更改了它的值并初始化了它。然后它没有相应地改变它的值。 然后,当我在控制台上打印响应正文时,它显示了错误状态错误:不允许http请求 import 'package:http/http.dart'; import 'package:intl/intl.dart'; import 'dart:convert'; class Wo
import 'package:http/http.dart';
import 'package:intl/intl.dart';
import 'dart:convert';
class WorldTime{
String location; //location name for UI
String time; // the time in that location
String flag;//url to an asset flag icon
String url; //location url for api endpoints
bool isDayTime=true;
WorldTime({ this.location,this.flag,this.url});
Future<void> getTime() async {
try{
//make the request
Response response = await
get('http://worldtimeapi.org/api/timezone/$url');
print(response.body);
Map data=jsonDecode(response.body);
//get properties from data
String datetime=data['datetime'];
String offset = data['utc_offset'].substring(1,3);
//create date time object
DateTime now=DateTime.parse(datetime);
now = now.add(Duration(hours: int.parse(offset)));
//set time property
isDayTime= now.hour>4 && now.hour<18?true:false;
time=DateFormat.jm().format(now);
}
catch(e){
print('caught error: $e');
time='could not get time data';
}
}
}
导入“包:http/http.dart”;
导入“包:intl/intl.dart”;
导入“dart:convert”;
阶级世界时间{
字符串位置;//用户界面的位置名称
字符串时间;//该位置的时间
字符串标志;//指向资产标志图标的url
字符串url;//api端点的位置url
bool isDayTime=true;
WorldTime({this.location,this.flag,this.url});
Future getTime()异步{
试一试{
//提出请求
响应=等待
得到('http://worldtimeapi.org/api/timezone/$url');
打印(响应.正文);
Map data=jsonDecode(response.body);
//从数据中获取属性
字符串datetime=data['datetime'];
字符串偏移量=数据['utc_offset']。子字符串(1,3);
//创建日期时间对象
DateTime now=DateTime.parse(DateTime);
now=now.add(持续时间(小时:int.parse(offset));
//设置时间属性
isDayTime=now.hour>4&&now.hour您应该使用安全http(https://) 更改此项:
Response response = await
get('http://worldtimeapi.org/api/timezone/$url');
为此:
Response response = await
get('https://worldtimeapi.org/api/timezone/$url');
你确定你当前定义了url和api密钥吗?是的,因为它已经正常工作了好几个月了…我上传到github后没有更改任何内容。它仍然不工作。对你发布在github上的数据。你没有授予http请求权限。你需要从github授予权限。你的问题不在这里,你需要授予github权限这能回答你的问题吗?