简体   繁体   English

如何使用 Flutter 通过 class 名称或 ID 从 url 获取数据?

[英]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.

相关问题 如何使用Dart / Flutter从API提取数据? - How do I fetch data from an API using dart/flutter? 如何从api获取此数据并将其显示在数据表颤动中 - How can I fetch this data from api and display it in data table flutter 如何从 flutter api 获取数据,参数为主体/原始数据 - how can I fetch data from flutter api with parameters as body/raw data 当我必须在循环中使用 get 请求获取此数据时,如何将数据放入 class object? - How can I put data into class object when I have to fetch this data using get request in loop? 在 Flutter 中,我如何使用 API 和 GetX 在 JASON 中获取数据 - In Flutter how I can fetch data inside a JASON with API and GetX 如何使用 Fetch 在 React 中从每个专辑中获取具有偶数 ID 的第一张图片? - How can I get the 1st image from each album with an even ID in React using Fetch? 如何在StackAPI上使用用户ID提取数据? - How do I fetch data using user ID on StackAPI? 如何在reactJS中使用id从API中获取数据? - How to fetch data from API using id in reactJS? 如何使用 URL 从数据库中检索数据 - How can I retrieve Data from a database by using the URL 如何使用flutter从https获取数据 - How to fetch data from https with flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM