我如何使用Flutter通过类名或id从url获取数据?

j8ag8udp  于 2023-01-27  发布在  Flutter
关注(0)|答案(2)|浏览(158)

如问题标题所示,我需要在Flutter中通过类名id从URL**中获取数据。有些数据我没有得到JSON数据,我需要它们在我的应用程序上显示。我可以使用下面的代码获取数据,但我无法通过类名或id获取任何数据,因为我不知道我需要使用什么。请帮助我

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()),
      ),
    );
  }
}
bsxbgnwa

bsxbgnwa1#

这是http package包。
下面是获得响应完整方法。

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;
    });
0mkxixxg

0mkxixxg2#

这个错误是因为不是你的手机问题,也许这也是一个问题,但按照我的代码;
根据我的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

在应用级build.gradle中,

defaultConfig {
    
    applicationId "com.example.emito"
    minSdkVersion localProperties.getProperty('flutter.minSdkVersion').toInteger()
   targetSdkVersion localProperties.getProperty('flutter.targetSdkVersion').toInteger()
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
}

如果您正在将本地属性.getProperty(“flutter. minSdkVersion”).toInteger()设置为本地属性.getProperty(“flutter. compileSdkVersion”).toInteger()
则会发生此类错误。请确保您的默认配置。
就这样谢谢

相关问题