【问题标题】:Flutter - Getting HTTP Response from url_launcherFlutter - 从 url_launcher 获取 HTTP 响应
【发布时间】:2018-10-23 19:17:45
【问题描述】:

在 Flutter 中,使用 url_launcherFIG A)将用户带到一个网站很容易。我正在寻找一种在完成/失败时返回 HTTP 状态代码的方法。理想情况下,这将在文本输出的字符串中返回。

图一;

_launchURL() async {
  const url = 'https://google.com/';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

谢谢

【问题讨论】:

    标签: http dart flutter http-status-codes


    【解决方案1】:
    import 'package:http/http.dart' as http;
    
    void _loadFromUrl(String url) async {
      http.Response response = await http.get(url);
      if (response.statusCode == 200) {...}
      else {...}
    

    希望对你有帮助

    【讨论】:

    • 我会尽快回复您。谢谢
    • 这行得通!谢谢安德烈。但是有一个问题,我怎样才能将状态码作为字符串返回?这就是我需要它的目的:print('All Good! This link returned status code' + response.statusCode);
    • print('All Good! This link returned status code ${response.statusCode}');
    猜你喜欢
    • 1970-01-01
    • 2022-06-28
    • 2021-02-04
    • 1970-01-01
    • 2012-11-07
    • 2018-08-28
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多