[英]How can I fetch data from url by class name or id using Flutter?
As you see in the title of the question, I need to fetch data from URL by class name or id in Flutter. There is some data I haven't got its JSON data, and I need them to display on my app.正如您在问题标题中看到的,我需要通过class 名称或 Flutter 中的ID从 URL获取数据。有些数据我还没有得到它的 JSON 数据,我需要它们显示在我的应用程序上。 I could fetch data using the code in the below, but I can't fetch any data by class name or id because I don't know what I need to use.我可以使用下面的代码获取数据,但我无法通过 class 名称或 ID 获取任何数据,因为我不知道我需要使用什么。 Please help me请帮我
class _MyHomePageState extends State<MyHomePage> {
String stringResponse;
Future fetcData() async {
http.Response response;
response = await http.get("www.example.com"); //Consider that the URL has just a string such as "This is a string."
if (response.statusCode == 200) {
setState(() {
stringResponse = response.body;
});
} else {
}
}
@override
void initState() {
// TODO: implement initState
fetcData();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Http request"),
),
body: Center(
child: Text(stringResponse.toString()),
),
);
}
}
here is package http package .这里是 package http package 。
here is a complete method to get a response.这是获得响应的完整方法。
HttpClient client = new HttpClient()
..badCertificateCallback =
((X509Certificate cert, String host, int port) => true);
return client.getUrl(Uri.parse(url)).then((HttpClientRequest request) {
request.headers.set('content-type', 'application/json-patch+json');
request.headers.set('accept', 'application/json');
var response = request.close().timeout(new Duration(seconds: 5));
return response;
}).then((HttpClientResponse response) {
var result = response.transform(utf8.decoder).join();
return result;
}).catchError((onError) {
var result = onError.transform(utf8.decoder).join();
return result;
});
This error is because not your phone issue, Maybe that's also an issue, But follow my code;这个错误是因为不是你的手机问题,也许这也是一个问题,但请按照我的代码进行操作;
Following my local.properties file按照我的 local.properties 文件
sdk.dir=/Users/gloify/Library/Android/sdk
flutter.sdk=/Users/gloify/Downloads/flutter
flutter.buildMode=debug
flutter.versionName=1.0.0
flutter.versionCode=1
flutter.minSdkVersion=27
flutter.targetSdkVersion=33
flutter.compileSdkVersion=33
and in app level build.gradle,在应用级别 build.gradle 中,
defaultConfig {
applicationId "com.example.emito"
minSdkVersion localProperties.getProperty('flutter.minSdkVersion').toInteger()
targetSdkVersion localProperties.getProperty('flutter.targetSdkVersion').toInteger()
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
if you are setting localProperties.getProperty('flutter.minSdkVersion').toInteger() as localProperties.getProperty('flutter.compileSdkVersion').toInteger()如果您将 localProperties.getProperty('flutter.minSdkVersion').toInteger() 设置为 localProperties.getProperty('flutter.compileSdkVersion').toInteger()
then this type of error will occur.那么就会出现这种类型的错误。 Make sure your default config.确保您的默认配置。
That's it Thank you就这样谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.