Api 使用颤振实时获取数据
我的应用程序中有一个问题,它通过从另一个网站获取数据来获取数据。我希望我的屏幕能够实时显示数据,这样,如果网站发生任何更改,它就会显示我的应用程序中的更改,而无需单击更新或执行任何其他操作 我试图利用未来,但我不知道如何利用它。这是我的代码:Api 使用颤振实时获取数据,api,flutter,dart,flutter-dependencies,Api,Flutter,Dart,Flutter Dependencies,我的应用程序中有一个问题,它通过从另一个网站获取数据来获取数据。我希望我的屏幕能够实时显示数据,这样,如果网站发生任何更改,它就会显示我的应用程序中的更改,而无需单击更新或执行任何其他操作 我试图利用未来,但我不知道如何利用它。这是我的代码: import 'dart:convert'; import 'dart:io'; import 'package:http/http.dart' as http; class FetchDataAPI { get(String url, String
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
class FetchDataAPI {
get(String url, String function, Map<String, String> header) async {
List Response = List();
Response.clear();
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
// Internet connected
try {
var resBody = await http.get(
url,
headers: header,
);
print(resBody.statusCode);
// 200
if (resBody.statusCode == 200) {
Response = [json.decode(resBody.body)];
return Response ;
}
// Not 200
else {
Response = [
{
"exception": false,
"statusCode": resBody.statusCode,
// Body here depend on response post man
"body": json.decode(resBody.body),
"message": null
}
];
return Response;
}
} catch (e) {
Response = [
{
"exception": true,
"statusCode": null,
"body": null,
"message": e.toString()
}
];
return Response;
}
} else {
// Internet not connected
Response = [
{
"exception": true,
"statusCode": null,
"body": null,
"message": "No internet conected."
}
];
return Response;
}
} on SocketException catch (e) {
Response = [
{
"exception": true,
"statusCode": null,
"body": null,
"message": "No internet conected."
}
];
return Response;
}
}
}
导入'dart:convert';
导入“dart:io”;
将“package:http/http.dart”导入为http;
类FetchDataAPI{
异步获取(字符串url、字符串函数、映射头){
列表响应=列表();
Response.clear();
试一试{
最终结果=等待InternetAddress.lookup('google.com');
if(result.isNotEmpty&&result[0].rawAddress.isNotEmpty){
//互联网连接
试一试{
var resBody=wait http.get(
网址,
标题:标题,
);
打印(resBody.statusCode);
// 200
如果(resBody.statusCode==200){
Response=[json.decode(resBody.body)];
返回响应;
}
//不是200
否则{
答复=[
{
“例外”:假,
“statusCode”:resBody.statusCode,
//这里的尸体取决于反应员
“body”:json.decode(resBody.body),
“消息”:空
}
];
返回响应;
}
}捕获(e){
答复=[
{
“例外”:正确,
“状态代码”:空,
“主体”:空,
“消息”:例如toString()
}
];
返回响应;
}
}否则{
//互联网未连接
答复=[
{
“例外”:正确,
“状态代码”:空,
“主体”:空,
“消息”:“未连接互联网。”
}
];
返回响应;
}
}关于SocketException捕捉(e){
答复=[
{
“例外”:正确,
“状态代码”:空,
“主体”:空,
“消息”:“未连接互联网。”
}
];
返回响应;
}
}
}
您应该使用